Created
April 3, 2012 15:29
-
-
Save piercemoore/2292935 to your computer and use it in GitHub Desktop.
Simple Recursion Function within a Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Foo { | |
| function __construct( $array ) { | |
| $this->storage = $this->store($array); | |
| } | |
| function store( $array ) { | |
| foreach( $array as $k => $v ) { | |
| if( is_array( $v )) { | |
| $output[$k] = $this->store( $v ); | |
| } else { | |
| $output[$k] = $v; | |
| } | |
| } | |
| return $output; | |
| } | |
| function show() { | |
| print_r($this->storage); | |
| } | |
| } | |
| $bar = array("One","Two","Three","Four"); | |
| $baz = array( | |
| "One" => array( | |
| "OneA", | |
| "OneB" => array( | |
| "OneAA", | |
| "OneBB" | |
| ) | |
| ), | |
| "Two" => array( | |
| "TwoA" | |
| ) | |
| ); | |
| print "<pre>"; | |
| $f = new Foo($bar); | |
| $f->show(); | |
| $g = new Foo($baz); | |
| $g->show(); | |
| print "</pre>"; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment