1

I have array which conatins (MANY) assoc arrays with values. I want to merge those nested arrays into single array. I tried call_user_func_array('array_merge', $bigArray), but this will write into same keys, since they are assoc and they repeat. So I need something that ignores array keys and just merges values.

My array:

[
    ['a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc'],
    ['a' => 'ddd', 'b' => 'eee', 'c' => 'fff'],
]

Desired result:

['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
Livia Martinez
  • 155
  • 2
  • 9

2 Answers2

1
$newArr = array();
foreach ($bigArray as $tmp) {
  $newArr = array_merge($newArr, array_values($tmp));
}

// $newArr holds your desired data
print_r($newArr);
colburton
  • 4,685
  • 2
  • 26
  • 39
0

You can use array_reduce in conjunction with array_values and array_merge:

$result = array_reduce($array, function ($r, $v) {
    return $r = array_merge($r, array_values($v));
});
hindmost
  • 7,125
  • 3
  • 27
  • 39