Created
November 4, 2013 22:25
-
-
Save tournasdim/7310265 to your computer and use it in GitHub Desktop.
A simple example of SPL's RecuriseveArrayIterator
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 | |
$myData = array( | |
['name' => 'dimitrios' , | |
['address' => 'SomeAddress' , 'country' => 'Greece' , ['phone'=> 4568765 , 'mobile' => 76599585]]] , | |
['name' => 'Jhon' ,'address' => 'Alimos' , 'country' => 'France' , 'phone' => 6577474] , | |
['name' => 'Justin' , 'address' => 'Sint Johnson ' , 'country' => 'UK' , 'phone' => '8875775'] | |
); | |
function recurseData($arr){ | |
$iter = new RecursiveArrayIterator($arr); | |
while($iter->valid()) | |
{ | |
if($iter->hasChildren()){ | |
echo "<ul><li>".$iter->key()." => "; | |
recurseData($iter->getChildren()); | |
echo "</li></ul>"; | |
}else{ | |
echo "<ul> | |
<li>".$iter->key()." => ".$iter->current()."</li> | |
</ul>"; | |
} | |
$iter->next(); | |
} | |
} | |
recurseData($myData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment