Home > Back-end >  Merge arrays in php with the same index
Merge arrays in php with the same index

Time:03-24

I have two arrays which I want to combine taking into account the index of one, like this:

Array 1:

Array
(
    [1] => 10
    [2] => 2
    [3] => 5
    [4] => 15
    [5] => 5
    [6] => 20
    [7] => 4
    [8] => 1
    [9] => 2
    [10] => 4
    [11] => 8
    [12] => 15
    [13] => 10
    [14] => 10
    [15] => 10
    [16] => 2
    [17] => 1
    [18] => 3
)

Array 2:

Array
(
    [0] => Array
        (
            [1] => 189.84-1
        )

    [1] => Array
        (
            [1] => 170.856-2
        )

    [2] => Array
        (
            [2] => 255.08-1
        )

    [3] => Array
        (
            [2] => 1132.6-2
        )

    [4] => Array
        (
            [3] => 138.82-1
        )

    [5] => Array
        (
            [3] => 124.938-2
        )

    [6] => Array
        (
            [4] => 163.66-1
        )

    [7] => Array
        (
            [4] => 147.294-2
        )

    [8] => Array
        (
            [5] => 222.57-1
        )

    [9] => Array
        (
            [5] => 200.313-2
        )

    [10] => Array
        (
            [6] => 97.18-1
        )

    [11] => Array
        (
            [6] => 87.462-2
        )

    [12] => Array
        (
            [7] => 116.67-1
        )

    [13] => Array
        (
            [7] => 105.003-2
        )

    [14] => Array
        (
            [8] => 679.55-1
        )

    [15] => Array
        (
            [8] => 611.595-2
        )

    [16] => Array
        (
            [9] => 474.65-1
        )

    [17] => Array
        (
            [9] => 427.185-2
        )

    [18] => Array
        (
            [10] => 549.19-1
        )

    [19] => Array
        (
            [10] => 494.271-2
        )

    [20] => Array
        (
            [11] => 109.88-1
        )

    [21] => Array
        (
            [11] => 98.892-2
        )

    [22] => Array
        (
            [12] => 153.78-1
        )

    [23] => Array
        (
            [12] => 138.402-2
        )

    [24] => Array
        (
            [13] => 549.19-1
        )

    [25] => Array
        (
            [13] => 494.271-2
        )

    [26] => Array
        (
            [14] => 16.53-1
        )

    [27] => Array
        (
            [14] => 14.877-2
        )

    [28] => Array
        (
            [15] => 28.11-1
        )

    [29] => Array
        (
            [15] => 25.299-2
        )

    [30] => Array
        (
            [16] => 62.43-1
        )

    [31] => Array
        (
            [16] => 56.187-2
        )

    [32] => Array
        (
            [17] => 433.55-1
        )

    [33] => Array
        (
            [17] => 390.195-2
        )

    [34] => Array
        (
            [18] => 52.1-1
        )

    [35] => Array
        (
            [18] => 46.89-2
        )

)

The index of the first array will be the index of my third resulting array.

expected result:

Array
    (
        [1] => Array
            (
                [0] => 189.84-1
                [1] => 170.856-2
                [2] => 10
            )
    
        [2] => Array
            (
                [0] => 255.08-1 
                [1] => 1132.6-2
                [2] => 2
            )
    
        [3] => Array
            (
                [0] => 138.82-1 
                [1] => 124.938-2
                [2] => 5
            )
        more elements...
    

I tried something like this and failed to reach the desired result.

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

another one:

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

can anyone give a different solution? pls

CodePudding user response:

Hopefully this gets you started, I think you're just missing a foreach!

$array1 = [
    1 => 12,
    2 => 23,
    3 => 34
];

$array2 = [
    1 => [
        1 => 2,
        3 => 5
    ],
    2 => [
        1 => 4,
        4 => 3
    ]
];

$output = [];
foreach($array1 as $k => $v)
    $output[$k][] = $v;

foreach($array2 as $k => $v)
    foreach($v as $kk => $vv)
        $output[$k][] = $vv;

print_r($output);
  • Related