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?

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

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

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.