0

I have two arrays like:

$a = [
    0 => [
        'price' => 5.5
    ],
    1 => [
        'price' => 6.0
    ],
    2 => [
        'price' => 6.2
    ],
    3 => [
        'price' => 6.5
    ],
];

$b = [
    0 => [
        'color' => 'RED'
    ],
    1 => [
        'color' => 'WHITE'
    ],
    2 => [
        'color' => 'BLUE'
    ],
    3 => [
        'color' => 'RED'
    ],
];

I should have this response:

Array
(
    [0] => Array
        (
            [price] => 5.5
            [color] => RED
        )
    [1] => Array
        (
            [price] => 6
            [color] => WHITE
        )
    [2] => Array
        (
            [price] => 6.2
            [color] => BLUE
        )
    [3] => Array
        (
            [price] => 6.5
            [color] => RED
        )
)

I heard about the function: array_merge_recursive but the response wasn't the requiered:

Array
(
    [0] => Array
        (
            [price] => 5.5
        )
    [1] => Array
        (
            [price] => 6
        )
    [2] => Array
        (
            [price] => 6.2
        )
    [3] => Array
        (
            [price] => 6.5
        )
    [4] => Array
        (
            [color] => RED
        )
    [5] => Array
        (
            [color] => WHITE
        )

    [6] => Array
        (
            [color] => BLUE
        )
    [7] => Array
        (
            [color] => RED
        )
    )

so I decided to write my own function:

function merge ($a, $b) {
    $keys = array_keys($a);
    foreach ($keys as $value) {
        if (isset($b[$value])) {
            $tmp = array_keys($b[$value]);
            foreach ($tmp as $val){
                $a[$value][$val] = $b[$value][$val];
            }
        }
    }
    return $a;
}
print_r(merge($a, $b));

and I got the proper response:

Array
(
    [0] => Array
        (
            [price] => 5.5
            [color] => RED
        )
    [1] => Array
        (
            [price] => 6
            [color] => WHITE
        )
    [2] => Array
        (
            [price] => 6.2
            [color] => BLUE
        )
    [3] => Array
        (
            [price] => 6.5
            [color] => RED
        )
)

The problem is that it works fine for little arrays but doesn't work good for big arrays, so my question is: how could I optimize the function? because the complexity would grow depending on merged keys.

Using PHP 7.0

0

3 Answers 3

4

You can use array_replace_recursive() instead.

array_replace_recursive($a, $b);

Demo: https://3v4l.org/bFIZ2

Sign up to request clarification or add additional context in comments.

2 Comments

Probably faster, I forgot that one.
Yes this is a lot faster and complies perfectly!
4

You need to check that they are the same length and then one simple foreach is all that is needed:

foreach($a as $k => $v) {
    $result[$k] = array_merge($v, $b[$k]);
}

2 Comments

there can be several keys to merge, and the array lengths may not be the same.
Magnus has the best one.
0

the simple solution might be,

$c = [];

for($i=0; $i<count(array_keys($a)); $i++) {
  $c[$i] =array_merge($a[$i], $b[$i]);
}

print_r($c);

https://repl.it/KJNL

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.