Jump to content

Create Multidimensional Array From Array Values


berridgeab

Recommended Posts

Hello

 

This is kinda hard to explain. I need to take a flat 1D array full of values and turn it into a multidimensional array.

 

This is the original array structure. The keys in $originalArray are not related to the structure of the desired array.

 

$originalArray = array(
 0 => array(0),
 1 => array(0,1),
 2 => array(0,2),
 3 => array(0,2,3),
 4 => array(0,4));

 

This is the desired array structure

 

$desiredArray = array(
 0 => array(
	 1 => array(),
	 2 => array(
		 3 => array()),
	 4 => array()));

 

Basically each sequential value in the $originalArray[X] must be nested into a new level.

 

I.e. 0 is the outer most key. The next level will contain 1,2 and 4. The next level (of key 2) will only contain 3. This should recurse and nest for each value,

 

Unfortunately I cannot do this. I know its something to do with references but I can't figure it out. Any ideas?

Link to comment
Share on other sites

No not homework it is for the job I do however I'm stuck. Ive been at it for 4 hours. I haven't posted my own example code becuase nothing I have done has got close to what I want. It could be done multiple ways im just struggling to understand the logic behind it. I know I will need pointers. The closest I have got is an unrelated answer on stack overflow.

 

$originalArray = array(0,1,2,3);
$desiredArray = array();
$arrayPointer =& $desiredArray;

foreach ($originalArray as $arrayValue) {
$arrayPointer[$arrayValue] = array();
$arrayPointer	 =& $arrayPointer[$arrayValue];
}

//Output of $desiredArray
Array
(
[0] => Array
 (
	 [1] => Array
		 (
			 [2] => Array
				 (
					 [3] => Array
						 (
						 )
				 )
		 )
 )
)

 

This gives me the desired array as long as I only work on one array. As soon as I try to put it in a loop (like below) with the remaining arrays, it goes wrong.

 

$originalArray = array(
0 => array(0),
1 => array(0,1),
2 => array(0,2),
3 => array(0,2,3),
4 => array(0,4));

$desiredArray = array();
$arrayPointer =& $desiredArray;
foreach($originalArray as $singleArray)
{
foreach ($singleArray as $arrayValue)
{
$arrayPointer[$arrayValue] = array();
 $arrayPointer	 =& $arrayPointer[$arrayValue];
}
}

//Output of $desiredArray too big to show

Link to comment
Share on other sites

Sorted, slight modification to the above code from StackOverflow finally got the desired structure.

 

$originalArray = array(
0 => array(0),
1 => array(0,1),
2 => array(0,2),
3 => array(0,2,3),
4 => array(0,4));
$desiredArray = array();
foreach($originalArray as $oneDArray)
{
$arrayPointer =& $desiredArray;
foreach($oneDArray as $arrayValue)
{
 if(!is_array($arrayPointer[$arrayValue]))
 {
	 $arrayPointer[$arrayValue] = array();
 }
 $arrayPointer = &$arrayPointer[$arrayValue];
}
}
//Output
Array
(
[0] => Array
 (
	 [1] => Array
		 (
		 )
	 [2] => Array
		 (
			 [3] => Array
				 (
				 )
		 )
	 [4] => Array
		 (
		 )
 )
)

Link to comment
Share on other sites

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.