Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.