starling Posted May 12, 2008 Share Posted May 12, 2008 [search]PHP Multidimensional Array to Readable List[/search]Ive been racking my brain on other problems all day and I cant bring myself to figure this one out on my own. /Tired brain. I want to convert a multidimensional array (of any size, it is not fixed) to a human readable list, such as: - Top - Child - Child of Child - Child 2 I have it sort of working except I cant figure out how to return the counter to the "starting" position from within a parent item. Meaning I get this: -Top - Child - Child of Child - Grandchild - This one should be a child of 'Child' rather than looking like one of 'Child of Child' And my function now... function output( $array, $space ) { foreach ( $array as $item => $data ) { if ( $data['parent'] == 0 ) { $inParent= 0; $space= 0; } echo str_repeat( " ", $space ).$data['name']."<br />"; if ( !empty( $data['children'] ) ) { $space++; output( $data['children'], $space ); } } } And an example array: $array= array( [1] => array( 'name' => 'Top', 'parent' => 0, 'children' => array( [2] => array( 'name' => 'Child', 'parent' => 1, 'children' => array(...) ) , [5] => array( 'name' => 'Child', 'parent' => 1, 'children' => array(...) ) , ) ) So it doesnt really matter what the names etc are, it just needs to be able to print the array out, sort of like what you get with print_r() but minus all the ugly php-y stuff. Also, the possibility to adapt this function to wrap each element in any sort of HTML etc is needed. I am sure there is a really simple solution to this, but its not happening for me today. Thanks! Link to comment https://forums.phpfreaks.com/topic/105247-solved-multidimensional-array-to-readable-list/ Share on other sites More sharing options...
Orio Posted May 12, 2008 Share Posted May 12, 2008 Thought of using print_r()? Example: <?php $array = array("one" => array(1,2,3,array(1,2)), "two" => 2); echo "<pre>"; print_r($array); echo "</pre>"; ?> Output: Array ( [one] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => Array ( [0] => 1 [1] => 2 ) ) [two] => 2 ) Orio. Link to comment https://forums.phpfreaks.com/topic/105247-solved-multidimensional-array-to-readable-list/#findComment-538921 Share on other sites More sharing options...
starling Posted May 12, 2008 Author Share Posted May 12, 2008 No, thats exactly what I do NOT need. I need a function that will do what print_r() does minus the ugly Array and key etc junk. I need it to be able to output the contents of the array as a list, but also be able to wrap each element in any content. Think categories and sub categories, so the array can be output as a regular list or even wrapped with <ul>/<li> tags (or anything else). Link to comment https://forums.phpfreaks.com/topic/105247-solved-multidimensional-array-to-readable-list/#findComment-539028 Share on other sites More sharing options...
starling Posted May 12, 2008 Author Share Posted May 12, 2008 OK got it. <?php function output( $array, $space = 0 ) { foreach ( $array as $item => $data ) { echo str_repeat( " ", $space ).$data['name']."<br />"; if ( !empty( $data['children'] ) ) { output( $data['children'], $space + 1 ); } } } ?> Link to comment https://forums.phpfreaks.com/topic/105247-solved-multidimensional-array-to-readable-list/#findComment-539037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.