3

I want to add the values of array b to array a:

$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];

Result should be:

$result = [[1, 2, 3],[4, 5, 6],[7, 8, 9]];

I am trying this (and lots of other stuff) but don't get it.

foreach ($a as $el) {
    $i = 0; 
    $el[] = $b[$i];
    $i++;
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Jan Tenner
  • 33
  • 2

5 Answers5

5

Here we are using array_walk to achieve desired output. Hope this will be helpful.

Try this code snippet here

<?php
ini_set('display_errors', 1);
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];

array_walk($a,function(&$value,$key) use($b){
    array_push($value, $b[$key]);
});
print_r($a);
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • 1
    Your solution should be faster than mine. – Danielius Jul 17 '17 at 15:04
  • 1
    I am unsure why this is voted up more than the `for loop` solution. This is alot more complicated - even using references. I guess because it looks more fancy? – Xatenev Jul 17 '17 at 15:10
  • @Xatenev See i gave an alternative approach to achieve desired output that's it. I myself upvoted `for-loop` approach. – Sahil Gulati Jul 17 '17 at 15:13
  • 1
    @Xatenev isn't this a faster solution than mine? `array_walk` should work faster. Of course, if an array is only as small as this one there is no difference. The second upvote is from me. – Danielius Jul 17 '17 at 15:14
  • @SahilGulati Sure I upvoted your answer aswell - I was just confused because the `for loop` is definetly a better solution in this case because its a lot more readable, and I'd definetly recommend it to OP – Xatenev Jul 17 '17 at 15:15
  • @Danielius Dunno that is super-microoptimization that doesn't matter. – Xatenev Jul 17 '17 at 15:16
  • @Xatenev Friend.. I usually try to answer a question in a different approach than others, so that OP can have no. of options to pick from and he can learn different things from different approaches, yes you are right `for-loop` can also be good. Thanks :) – Sahil Gulati Jul 17 '17 at 15:20
4

This should be as simple as:

$a = [[1, 2],[4, 5],[7, 8]];          
$b = [3, 6, 9];
foreach($a as $key => &$arr){
    $arr[] = $b[$key];
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Sotiris Kiritsis
  • 3,178
  • 3
  • 23
  • 31
3

This is not hard.

<?php
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
for($i = 0; $i < count($b);$i++) {
    array_push($a[$i],$b[$i]);
}
?>
Danielius
  • 852
  • 5
  • 23
0
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
$result = $a;
foreach ($a as $key => $val) {
    if(!empty($b[$key])) {
        array_push($result[$key], $b[$key]);
    }
}

var_export($result);
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
0

Here is the clever one-liner that no one else thought of:

Code: (Demo)

$a = [[1, 2],[4, 5],[7, 8]];          
$b = [3, 6, 9];

var_export(array_map('array_merge',$a,array_chunk($b,1)));

Output:

array (
  0 => 
  array (
    0 => 1,
    1 => 2,
    2 => 3,
  ),
  1 => 
  array (
    0 => 4,
    1 => 5,
    2 => 6,
  ),
  2 => 
  array (
    0 => 7,
    1 => 8,
    2 => 9,
  ),
)

This approach splits $b into the same structure as $a, then it is simple to merge them together.

If your input arrays are relatively small, a foreach() is probably the best choice. As your input array grows in size, I believe you will find that my approach will have increasing performance benefits over foreach() (though I'll be honest I am basing this on other similar answers I have provided and I haven't actually benchmarked this case).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136