0

I have this array $a:

[0] => Array
    (
        [param] => banana
        [value] => yellow
    )

[1] => Array
    (
        [param] => lemon
        [value] => 3
    )

[2] => Array
    (
        [param] => apple
        [value] => 18
    )

... and array $b:

[0] => Array
    (
        [param] => banana
        [value] => brown
    )

[1] => Array
    (
        [param] => orange
        [value] => 3
    )

[2] => Array
    (
        [param] => lemon
        [value] => 4
    )

[3] => Array
    (
        [param] => pear
        [value] => green
    )

Array $c should be a copy of $a, but, if $b contains certain params that are present in $a, the values from $a should be update with those from $b.

So $c would become:

[0] => Array
    (
        [param] => banana
        [value] => brown
    )

[1] => Array
    (
        [param] => lemon
        [value] => 4
    )

[2] => Array
    (
        [param] => apple
        [value] => 18
    )

Any tips?

3
  • use $a = array_column($a, null, 'param'); and $b in the same way. After, the task will be trivial Commented Aug 23, 2019 at 10:23
  • Only trivial to some I'm affraid :). Should I use foreach to go through array $a? If so, how can I define if the key is present in $b? Commented Aug 23, 2019 at 11:37
  • @binoculars I updated my answer. Once have a look. Commented Aug 23, 2019 at 11:40

2 Answers 2

1

You can use array-map with multiple arrays,

$c = array_filter(array_map(function ($a1, $b1) {
    if ($a1['param'] == $b1['param']) {
        $a1['value'] = $b1['value'];
        return $a1;
    } else {
        return $a1;
    }

}, $a, $b));

Demo

EDIT

$bData = array_column($b,'value','param'); // param as key and value as value
foreach($a as &$v){
    $v['value'] = ($bData[$v['param']] ?? $v['value']); // php 7+   
    //or $v['value'] = (!empty($bData[$v['param']]) ? $bData[$v['param']] :   $v['value']);    // php 5.6 or previous

}

Demo

Output

Array
(
    [0] => Array
        (
            [param] => banana
            [value] => brown
        )

    [1] => Array
        (
            [param] => lemon
            [value] => 4
        )

    [2] => Array
        (
            [param] => apple
            [value] => 18
        )

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

3 Comments

Are you sure that the same keys will be on the same positions? It's a strange approach
Unless we see more precision from OP. I have something in my mind.
Thanks for the suggestion, but as @splash58 mentioned, this only works if the keys are on the same position, which isn't necessarily the case. I'll update my question to reflect this.
0

    $a = ['banana' => 'yellow', 'apple' => 3, 'lemon' => 18];

    $b = ['banana' => 'brown', 'orange' => 3, 'lemon' => 4];

    $a_keys = array_keys($a);

    $c = $a;

    foreach ($a_keys as $key) {
        if(array_key_exists($key, $b)){
            $c[$key] = $b[$key];
        }
    }

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.