Today I was trying to find one small variable in a massive PHP array. However, var_dump and print_r weren’t outputting the contents of the array in a human readable way. So thanks to the RecursiveArrayIterator, I was able to spit out the variables in more legible manner.
Here’s how it went:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function recurse_array($arr){ $iter = new RecursiveArrayIterator($arr); echo " "; while($iter->valid()){ if($iter->hasChildren()){ echo "<ul><li>".$iter->key()." => "; recurse_array($iter->getChildren()); echo "</li></ul>"; }else{ echo "<ul> <li>".$iter->key()." => ".$iter->current()."</li> </ul>"; } $iter->next(); } echo ""; } recurse_array($thearray); |