-1

So I found an answer which is supposed to work, however it doesn't appear too...

Already accepted answer with the same issue.

I have got the following array called $banners:

[
  0 => [
    "bannerCustomTemplate" => 0,
    "bannerId" => 1,
    "bannerType" => 1,
    "bannerTitle" => "Merry",
    "bannerStrapline" => "Christmas",
    "bannerPeriod" => "2018-12-01 to 2018-12-10",
    "bannerText" => "Christmas opening hours"
  ],
  1 => [
    "bannerCustomTemplate" => 0,
    "bannerId" => 7,
    "bannerType" => 2,
    "bannerTitle" => "Easter",
    "bannerStrapline" => "Test",
    "bannerPeriod" => "2018-12-04 to 2018-12-12",
    "bannerText" => "dsadasdaas"
  ]
]

The answers I have read suggest $all_banners = call_user_func_array('array_merge', $banners);.

However this is giving me:

[
  "bannerCustomTemplate" => 0,
  "bannerId" => 7,
  "bannerType" => 2,
  "bannerTitle" => "Easter",
  "bannerStrapline" => "Test",
  "bannerPeriod" => "2018-12-04 to 2018-12-12",
  "bannerText" => "dsadasdaas"
]

Seems like it's just replacing rather than merging. Anyone got any ideas?

Edit

Just read the following comment

Little note here. The updated variant with unpacking array doesn't work with string keys. But the first one works perfect. Just keep in mind this. – Alliswell

So I have now updated my code with another solutions, with the same results.

Edit 2

Well, merging is merging not replacing. So what I expect is:

[
    "bannerCustomTemplate" => [ 0, 0 ],
    "bannerId" => [ 1, 7 ],
    "bannerType" => [ 1, 2 ],
    "bannerTitle" => [ "Merry", "Easter" ]
    "bannerStrapline" => [ "Christmas", "Test" ]
    "bannerPeriod" => [ "2018-12-01 to 2018-12-10", "2018-12-04 to 2018-12-12" ]
    "bannerText" => ["Christmas opening hours", "dsadasdaas" ]
]
Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • There's no simple built-in PHP function that would know to merge the values of identical keys into arrays of values. You'll need to do a bit more custom coding there. Also, the result should be an array of one array…!? – deceze Dec 03 '18 at 13:33
  • Is this answer approved falsely? It's the exact same issue. https://stackoverflow.com/questions/17041278/php-how-to-merge-arrays-inside-array – Martyn Ball Dec 03 '18 at 13:34
  • Yeah, seems not fit for the question. – deceze Dec 03 '18 at 13:35
  • Sorry can you clarify. The above linked question was attempting to do the exact same thing as me, and he accepted the answer. Would that answer not have worked? – Martyn Ball Dec 03 '18 at 13:37
  • 1
    As I said: That answer indeed does not seem to fit the question. It would not have worked. – deceze Dec 03 '18 at 13:37
  • You could greatly improve this question by telling us specifically what you expect as output. Pointing to another page is not enough; questions should be self-contained – Stephen R Dec 03 '18 at 13:58

1 Answers1

1

If all your arrays are known to contain the same keys in the same order, this is probably easiest:

$data = [
    ['foo' => 'bar', 'baz' => 42],
    ['foo' => 'baz', 'baz' => 69]
];

$result = array_combine(array_keys($data[0]), array_map(null, ...$data));

This uses the useful behaviour of array_map with null as the callback to take one element from each input array and return a new combined array.

deceze
  • 510,633
  • 85
  • 743
  • 889