1

I need to evenly/synchronously push values from my second array into the rows of my first array.

The arrays which have the same size, but with different keys and depths. The first is an array of rows and the second is a flat array.

$array1 = [
    12 => [130, 28, 1],
    19 => [52, 2, 3],
    34 => [85, 10, 5]
]

$array2 = [4, 38, 33]

Preferred result:

[
    12 => [130, 28, 1, 4],
    19 => [52, 2, 3, 38],
    34 => [85, 10, 5, 33]
]

(I would like to keep the same indices of array 1, however it is not mandatory.)

I have tried these methods, but none of them work because the first array keys are unpredictable.

$final = [];
foreach ($array1 as $idx => $val) {
    $final = [$val, $array2[$idx]];
}

Another:

foreach ($array1 as $index => $subArray) {
    $array1 [$index][] = $array2[$index];
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Myers
  • 148
  • 8

4 Answers4

2

An example using foreach

<?php

$a = [
    2 => [130, 28, 1, 1, 6],
    3 => [52, 2, 3, 3, 27]
];

$b = [5, 38];

$output = [];
$idx = 0;
foreach ($a as $key => $value) {
    $value[] = $b[$idx];
    $output[$key] = $value;
    ++$idx;
}

print_r($output);

Sandbox HERE

ikhvjs
  • 5,316
  • 2
  • 13
  • 36
2

You can loop $array1 using a foreach to get the current key $index

Get the value from $array2 by using a counter as the array key which, is incremented by 1 for every iteration.

Then add the value to the end of the current array.

$array1 = [
    2 => [130, 28, 1, 1, 6],
    3 => [52, 2, 3, 3, 27],
    13 => [41, 20, 27, 13, 37]
];
$array2 = [89, 99, 109];

$counter = 0;
foreach ($array1 as $index => $subArray) {
    $array1[$index][] = $array2[$counter++];
}

print_r($array1);

Output

Array
(
    [2] => Array
        (
            [0] => 130
            [1] => 28
            [2] => 1
            [3] => 1
            [4] => 6
            [5] => 89
        )

    [3] => Array
        (
            [0] => 52
            [1] => 2
            [2] => 3
            [3] => 3
            [4] => 27
            [5] => 99
        )

    [13] => Array
        (
            [0] => 41
            [1] => 20
            [2] => 27
            [3] => 13
            [4] => 37
            [5] => 109
        )

)

See a PHP demo.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    Haha, I didn't notice the difference of index is 2 :D – ikhvjs Feb 08 '22 at 15:57
  • works up to index 46. from index 47 it does not add any more! – Myers Feb 08 '22 at 16:12
  • @Myers 47 items means an index from 0 - 46 The arrays have an equal number of items right? – The fourth bird Feb 08 '22 at 16:18
  • No, only in array2 they are from 0 to 46, but in array1 it starts from 2 to 11, then 13 to 17, 19 to 23, then 25, then 27 to 33, 35 to 39, then 41 appears alone, to start from 44 to 46, in conclusion it has no even order, odd order, nothing, it is random. – Myers Feb 08 '22 at 16:24
  • In other words, it would look like this: At the first index of the first array insert the first index of the second array. At the second index of the first array insert the second index of the second array. INDEPENDENTLY OF THE NUMBER you have as index for the second array – Myers Feb 08 '22 at 16:44
  • @Myers I have updated it. Now it loops `$array1` and for every iteration, you use the counter to the item from `$array2` if that key exists. – The fourth bird Feb 08 '22 at 16:47
  • It works. but in each index it inserts twice the same value! – Myers Feb 08 '22 at 16:56
  • @Myers I don't get what you mean by that. It inserts the value only once right? – The fourth bird Feb 08 '22 at 17:18
  • The asker says: "_I have two arrays with the same size_". This snippet does not have two arrays of the same size. – mickmackusa Aug 30 '22 at 20:54
  • @mickmackusa Ah yes, now I see it..it should be value of `$array2` with an incremented index. – The fourth bird Aug 30 '22 at 21:51
2

Here is one way to do this:

$merged = array_map('array_merge', $array1, array_chunk($array2, 1));
$result = array_combine(array_keys($array1), $merged);

The second step with array_combine is necessary to reapply the non-sequential keys because array_map won't preserve them in the first step.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
1

Maintaining a counter while iterating is a simple way of accessing second array values while iterating the first. It is not necessary to make multiple passes of the arrays, just one iteration is all that is required.

Codes: (Demos)

  • a mapper:

    $i = -1;
    var_export(
        array_map(fn($row) => array_merge($row, [$array2[++$i]]), $array1)
    );
    
  • a looper:

    $i = -1;
    foreach ($array1 as &$row) {
        array_push($row, $array2[++$i]);
    }
    var_export($array1);
    
  • a walker:

    $i = -1;
    array_walk($array1, fn(&$row, $k) => array_push($row, $array2[++$i]));
    var_export($array1);
    

If you don't care about preserving the first array's keys in the result array, then you can simply use:

var_export(
    array_map('array_merge', $array1, array_chunk($array2, 1))
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136