用于使用具有相同键的不同数组值替换内部数组值的PHP数组方法

I'm trying to understand PHP array methods, so Id prefer using the array methods to solve this.

Here is my data:

$dataA =>
    array
      0 => 
        array
          'type' => string 'name' (length=4)
          'key' => string 'keywords' (length=8)
          'content' => string 'keywordA' (length=14)

$dataB =>
    array
      1 => 
        array
          'type' => string 'name' (length=4)
          'key' => string 'keywords' (length=8)
          'content' => string 'keywordB' (length=14)

What I'd like to do is have the two arrays merged and the final content key be:

$finalData =>
    array
      0 => 
        array
          'type' => string 'name' (length=4)
          'key' => string 'keywords' (length=8)
          'content' => string 'keywordB' (length=14)
                               ^-- notice here that the content has changed based on the fact that 'key' for both is 'keywords'

As you can see, the final content value is from $dataB.

Recursive replace does what you want

$finalData = array_replace_recursive($dataA, $dataB);

For this particular example, also plus operator will do what you want:

$finalData = $dataB + $dataA;

But you will have to specify arguments in different order.

There is no such built-in function. You need something to replace only one specific key, only when other is equal it's equivalent in second array, moreover there are associative arrays in zero-indexed ones and you need to compare items only in second-level. If you look at it after describing, you may notice that it is not general functionality that everybody needs, so it is not included in standard function set.

Assuming that key value is unique per array, here is solution without visible loops:

$getKey = function($item){ return $item['key']; };
$keysA = array_map($getKey, $dataA);
$keysB = array_map($getKey, $dataB);
$finalData = array_values(array_replace_recursive(
    array_combine($keysA, $dataA), 
    array_combine($keysB, $dataB) 
));

Make a copy of $dataB, then loop over $dataA. If value of $dataA is not found in $dataB, add it to your copy.