berridgeab Posted November 30, 2012 Share Posted November 30, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/271408-create-multidimensional-array-from-array-values/ Share on other sites More sharing options...
Hsnopi Posted November 30, 2012 Share Posted November 30, 2012 i think you'd need to walk the list, one at a time. this sounds like a walking the tree type problem. your recursion level will indicate how deep you need to be int he new data structure. Is this homework? Quote Link to comment https://forums.phpfreaks.com/topic/271408-create-multidimensional-array-from-array-values/#findComment-1396474 Share on other sites More sharing options...
berridgeab Posted November 30, 2012 Author Share Posted November 30, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271408-create-multidimensional-array-from-array-values/#findComment-1396484 Share on other sites More sharing options...
berridgeab Posted November 30, 2012 Author Share Posted November 30, 2012 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 ( ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/271408-create-multidimensional-array-from-array-values/#findComment-1396491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.