0

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

3
  • $result = $array_one; foreach($array_two as $idx => $val) { $result[$idx][] = $val; } how's that work for you? Copy array one as your "$result", then just keep adding to the array by the index indicated -- don't redefine it. Commented Mar 23, 2022 at 14:43
  • Error!: Fatal error: [] operator not supported for strings in Commented Mar 23, 2022 at 14:56
  • Oh right, the first array has scalars. Then, $result = array_map(fn($val) => [$val], $array_one); and the rest should work as intended. Sorry, can't be bothered testing since the sample data is in dump format, ie. not ready to play with. Commented Mar 23, 2022 at 16:05

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.