0

I'm trying to achieve something I naively believed would be simple: flattening a multidimensional array (which could have many nested levels) but still having arrays as a result. Ideally I'm looking for a function that can iterate through 10+ nested levels and can handle different set of keys (not necessarily always the same).

In short, turning this:

 Array
 (
 [0] => Array
 (
 [0] => Array
 (
 [index] => -1
 [qty] => 77
 [id] => 7
 )
 [1] => Array
 (
 [index] => -1
 [qty] => 83
 [id] => 8
 )
 )
 [1] => Array
 (
 [0] => Array
 (
 [index] => -1
 [qty] => 75
 [id] => 13
 )
 [1] => Array
 (
 [index] => -1
 [qty] => 60
 [id] => 14
 [msr] => g
 )
 )
 [2] => Array
 (
 [index] => -1
 [qty] => 10
 [id] => 12
 )
 )

Into this:

 Array
 (
 [0] => Array
 (
 [index] => -1
 [qty] => 77
 [id] => 7
 )
 [1] => Array
 (
 [index] => -1
 [qty] => 83
 [id] => 8
 )
 [2] => Array
 (
 [index] => -1
 [qty] => 75
 [id] => 13
 )
 [3] => Array
 (
 [index] => -1
 [qty] => 60
 [id] => 14
 [msr] => g
 )
 [4] => Array
 (
 [index] => -1
 [qty] => 10
 [id] => 12
 )
 )

This is what I had and thought would work, but I end up with a flat array with no key information (and if I want keys, every iteration overwrites the previous values and I end up with just the last array of them all):

function flatten_multi_array(array $array){
 $ret_array = array();
 foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
 $ret_array[] = $value;
 }
 return $ret_array;
}
asked Oct 2, 2011 at 22:25

4 Answers 4

1
function dig($source, &$out){
 foreach ($source as $k => $v){
 if (isset($v["index"]){
 $out[] = $v;
 } else {
 dig($v, $out);
 }
 }
}

and that's it.

usage:

$out = array();
$source = array(); // your magic nested array
dig($source, $out);

and now $out has what you need.

answered Oct 2, 2011 at 22:29
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for replying. But this didn't really work. I get a flattened array and lose all key information...
sure, it does merge subarrays, not maintaining key. if you need key, then just replace $out = $v with $out = array(..., "key" => $k); from the description it was not clear if either key is unique or should be maintained.
I already had a function that did that same thing (see edit), and if I want to have keys, since it doesn't have a second dimension, all values would be overwritten.
in your example, you are not preserving keys if we compare the input to output. so, I'm sorry but I'm losing the whole point of your question. perhaps you could create more simple but more abstract description of the problem. as for key, I would suggest just adding it as a record similar to "index", "qty", "id"..
Nevermind, you were right all along, but I initially missed the point of your function. Thanks so much for this!
1

If you're still looking for the RecursiveIteratorIterator approach, see the following:

foreach(new RecursiveIteratorIterator(new ParentIterator(new RecursiveArrayIterator($array)), RecursiveIteratorIterator::SELF_FIRST) as $value) {
 if (isset($value['index']))
 $ret_array[] = $value;
}

This should do it inside your function. See as well the demo.

Related: Quick Recursive search of all indexes within an array

answered Oct 3, 2011 at 0:14

1 Comment

Thanks! I really like this alternative approach!
0

Something like this untested code maybe...

$outArray = array();
foreach($originalArray as $nestedArray){
 foreach($nestedArray as $innerArray){
 $outArray[] = $innerArray;
 }
}
print_r($outArray);
answered Oct 2, 2011 at 22:32

1 Comment

I missed that part... scratch this then
0

I see that this was just answered. Well, I thought I'd contribute my solution since I did it. :P

$newArray = array();
function isMulti($k,$v){
 global $newArray;
 if(is_array($v)){
 foreach($v as $k2 => $v2){
 if(!is_array($v2)){
 $newArray[] = $v;
 break;
 }else{
 isMulti($k2,$v2);
 }
 }
 }
}
foreach($arrayInQuestion as $k => $v){
 isMulti($k,$v);
}
answered Oct 2, 2011 at 23:47

1 Comment

Well, at least mine works without having the 'index' keys. :P

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.