Jump to content

[SOLVED] Multidimensional Array to Readable List


starling

Recommended Posts

[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!

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.

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).

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 );
        }
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.