Jump to content

Building a multi-dimensional array


Mark W

Recommended Posts

Hi everyone :)

 

This has had me scratching my head for a while, hopefully someone can help.

 

I have an array which looks something like this:

array(
1	=> array(
	'id'		=> 1,
	'name'		=> 'Root',
	'parent'	=> 0,
),
2	=> array(
	'id'		=> 2,
	'name'		=> 'Child',
	'parent'	=> 1,
),
3	=> array(
	'id'		=> 3,
	'name'		=> 'Sub-Child',
	'parent'	=> 2,
),
)

 

I need to use the parent element to build a structure, so I'll end up with something like this:

array(
1	=> array(
	2	=> array(
		3	=> array(),
	),
),
)

 

The issue is that the structure could be infinitely deep. I can't work out a way to process it.

 

Anyone have any ideas?

Link to comment
https://forums.phpfreaks.com/topic/190956-building-a-multi-dimensional-array/
Share on other sites

Why? You already have the data in a format that should be usable, why make it more complicated?

 

Even though I think you are probably going about it wrong, I'll still provide a solution to what you asked. There is probably a more efficient solution, but htis works:

 

function convertArray($inputArray)
{
    end($inputArray);
    $lastArray = array();

    $outputArray = array();
    $outputArray[key($inputArray)] = current($inputArray);

    do
   {
        $lastArray = $outputArray;
      $outputArray = array();
        $outputArray[key($inputArray)] = current($inputArray);
      $outputArray[key($inputArray)][key($lastArray)] = current($lastArray);

   } while (prev($inputArray));

    return $outputArray;
}

$test = array(
    1 => array( 'id' => 1, 'name' => 'Root', 'parent' => 0 ),
    2 => array( 'id' => 2, 'name' => 'Child', 'parent' => 1 ),
    3 => array( 'id' => 3, 'name' => 'Sub-Child', 'parent' => 2 )
);

echo "<pre>";
print_r($test);
echo "\n\n";
print_r(convertArray($test));
echo "<pre>";

 

Output:

Original Array:
Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Root
            [parent] => 0
        )
    [2] => Array
        (
            [id] => 2
            [name] => Child
            [parent] => 1
        )
    [3] => Array
        (
            [id] => 3
            [name] => Sub-Child
            [parent] => 2
        )
)

Converted Array:
Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Root
            [parent] => 0
            [2] => Array
                (
                    [id] => 2
                    [name] => Child
                    [parent] => 1
                    [3] => Array
                        (
                            [id] => 3
                            [name] => Sub-Child
                            [parent] => 2
                            [3] => Array
                                (
                                    [id] => 3
                                    [name] => Sub-Child
                                    [parent] => 2
                                )
                        )
                )
        )
)

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.