Skip to content

Instantly share code, notes, and snippets.

@tournasdim
Created November 4, 2013 22:25
Show Gist options
  • Save tournasdim/7310265 to your computer and use it in GitHub Desktop.
Save tournasdim/7310265 to your computer and use it in GitHub Desktop.
A simple example of SPL's RecuriseveArrayIterator
<?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