1

I have an array like this:

array (size=4)
  0 =>
    array (size=4)
      key => value
      key => value
      key => value
      key => value
  1 =>
    array (size=2)
      key => value
      key => value
  2 =>
    array (size=1)
      key => value
  3 =>
    array (size=1)
      key => value

I want to flatten the array to be this:

array (size=4)
  key => value
  key => value
  key => value
  key => value
  key => value
  key => value
  key => value
  key => value

I have tried my own solutions using things like array_merge, array_walk_recursive, and RecursiveIteratorIterator with RecursiveArrayIterator. I also tried many of the solutions posted on similar questions on StackOverflow, but none of them work the way I would expect. They either do not preserve the key value pairs, or they give me an array that is identical to the original. My attempts look something like this:

$multidimensionalArray = array(stuff goes here);
$flatArray = array();

function flattenArray ($array) {
  foreach ($array as $key => $value) {
    if (is_array($value) {
      flattenArray($value);
    }
    else {
      $flatArray[$key] = $value;
    }
  }
}

flattenArray($multidimensionalArray);
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
user6679670
  • 25
  • 1
  • 7
  • Possible duplicate of [How to Flatten a Multidimensional Array?](http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – Prafulla Kumar Sahu Jan 26 '17 at 04:17
  • You may not be able to access the global variable `$flatArray` inside your function. Declare it as global inside or pass it by reference to the function – Kyle Domingo Jan 26 '17 at 04:48
  • You're right, I was not accessing the variable within the function. When I check the content of the $flatArray within the loop, I see that it is correctly flattening each sub array, but they are not joining together. I have tried using array_merge to correct this, but after the loops has finished I end up with just the final key value pair. – user6679670 Jan 27 '17 at 05:13

1 Answers1

1

I wonder what could go wrong with RecursiveArrayIterator, because you only needed to collect the key/value pairs from the iterator using the simplest foreach loop:

$a = [
  0 => ['a' => 1, 'b' => 2],
  1 => ['x' => 3, 'y' => 4],
  2 => 5,
  3 => ['m' => 6, ['k' => 7, 'n' => 8]],
];

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));

foreach ($it as $key => $value) {
  $result[$key] = $value;
}

print_r($result);

Output:

Array
(
    [a] => 1
    [b] => 2
    [x] => 3
    [y] => 4
    [2] => 5
    [m] => 6
    [k] => 7
    [n] => 8
)
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • When printing the result it does look correct, but when I check in a var_dump the keys were actually replaced with 0 through 7, while the values were preserved. – user6679670 Jan 27 '17 at 05:07
  • @user6679670, `var_dump` makes no difference unless it is a buggy `var_dump`, or `var_dump` is printing an object that incorrectly implements `__debuginfo` method. Example with `var_dump`: https://eval.in/725158 (as you can see, the keys are preserved) – Ruslan Osmanov Jan 27 '17 at 05:17
  • I do not think it is an issue with the var_dump, as when I try to reference the keys later on in my code, they are not there (just the index 0,1,2, etc.) I wound up flattening the keys and values separately, then using combine_array to join them together with the correct key/value pairs. – user6679670 Feb 02 '17 at 18:32
  • Silly me, I was not using the key/value pair correctly during the foreach loop. It is now working correctly. Thank you! – user6679670 Feb 02 '17 at 19:37
  • This is cool, Thanks, Well is this the only way to achieve this ? any other much simpler way ? – BlackBurn027 Oct 06 '17 at 03:04
  • @BlackBurn027, a simpler way would be to use a built-in function, but there is no such function, AFAIK. – Ruslan Osmanov Oct 06 '17 at 03:14
  • Ya i looked for built-in couldn't find any! Anyhow thanks this helped! – BlackBurn027 Oct 06 '17 at 04:39