0

I have multiple arrays structured like these:

$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];

meaning:

  • every array has the same value for each key (eg: array1 has value "1" for every item in the array) but there are no arrays sharing the same value (eg: if array1 has value 1, then none of the other arrays has value = 1)
  • the arrays may or may not share the same keys

I need to combine these arrays in a way that the final result is something like this:

$result = [
    "aaa" => [1,12],
    "bbb" => [1,12,15],
    "ccc" => [15],
];

meaning:

  • the final array must contain all the keys from the previous arrays
  • the value of the key is an array composed of all the values of the previous arrays that shared the same key

I know it's a bit messy, but I hope it is clear enough. I'm struggling to build the $result array. I tried merge, combine, intersect, but none of them seems to work. Is there a way to build the $result array without using a loop?

Thanks

3
  • Is there a way to build the $result array without using a loop? The short answer is no, there will always be some kind of 'looping' involved, whether in plain sight or hidden (in functions). Commented Sep 27, 2021 at 15:39
  • I don't think there's any built-in merging function that will do this. The loop should be pretty straightforward. Commented Sep 27, 2021 at 15:42
  • Start with foreach ([$array1, $array2, $array3] as $array) { foreach ($array as $key => $value) { ... } }. The ... pushes the value into the appropriate element of $result. Commented Sep 27, 2021 at 15:43

2 Answers 2

0

Does it match your goal ?

<?php
    $array1 = ["aaa" => 1, "bbb" => 1];
    $array2 = ["aaa" => 12, "bbb" => 12];
    $array3 = ["bbb" => 15, "ccc" => 15];

    $array = array_merge_recursive($array1, $array2, $array3);
    
    print_r($array);
?>

outputs

Array
(
    [aaa] => Array
        (
            [0] => 1
            [1] => 12
        )

    [bbb] => Array
        (
            [0] => 1
            [1] => 12
            [2] => 15
        )

    [ccc] => 15
)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi. thank you a lot, this looks to me like the best solution. Note: array_merge_recursive works well only with string keys, if you have numeric strings or int as keys, array_merge_recursive won't keep them. It's not an issue in my case anyway, thanks again
Glad to read it, thanks for your feedback and precision. In fact, don't repeat it to anyone, but I discover this function with you :D Happy coding !
0

Merge all of array into an mergedArray. Then use 2 foreach to set it.

<?php

$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];

$mergedArray = [$array1, $array2, $array3];


$result = [];
foreach ($mergedArray as $array) {
    foreach ($array as $key => $item) {
        $result[$key][] = $item;
    }
}

echo '<pre>';
print_r($result);
echo '</pre>';
exit;

?>

The result:

Array
(
    [aaa] => Array
        (
            [0] => 1
            [1] => 12
        )

    [bbb] => Array
        (
            [0] => 1
            [1] => 12
            [2] => 15
        )

    [ccc] => Array
        (
            [0] => 15
        )

)

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.