Evenly push values from a flat array into same positioned rows of a 2d array

132 Views Asked by At

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];
}
4

There are 4 best solutions below

0
Don't Panic On BEST ANSWER

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.

0
ikhvjs On

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

10
The fourth bird On

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.

0
mickmackusa On

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))
);