根据另一个数组中的值替换数组中的值

Replacing values in an array basing on values from another

具有以下 2 个数组:

[
    {"id":1,"value":40},
    {"id":2,"value":30}
]

并且:

[
    {"userId":1,"worth":20},
    {"userId":2,"worth":10}
]

我最终想要的是下面的结果:

[
    {"id":1,"value":20},
    {"id":2,"value":10}
]

所以在这里,我想根据 ìd` 将第一个数组中的值替换为第二个数组中的值。我做了类似的东西:

foreach ($array2 as $k => $v) {
    array_filter($array1), function($item) use($value) {
        $item['id'] == $v['userId'] ? $item['value'] = $v['worth'] :$item['value'] = $item['value'];
    });
}

它适用于那些给定的数组,但如果你的数组包含超过 100 万条数据,它将永远无法完成! 问题是,是否有一些 PHP 函数可以完成这项艰巨的工作?

更新

数组格式已更新,现在我应该使用以下格式:

[
    0 => stdClass {"id":1,"value":40},
    1 => stdClass {"id":2,"value":30}
]

并且:

[
    0 => Statement {"userId":1,"worth":20},
    1 => Statement {"userId":2,"worth":10}
]

Var_dump的结果:

数组 1:

array (size=2)
  0 => 
    object(stdClass)[2721]
      public 'value' => float 84
      public 'id' => int 1229
  1 => 
    object(stdClass)[2707]
      public 'value' => float 144
      public 'id' => int 1712

数组 2:

array (size=2)
  0 => 
    object(Bank\Accounting\Statement)[2754]
      public 'worth' => float 572
      public 'userId' => int 1229
  1 => 
    object(Bank\Accounting\Statement)[2753]
      protected 'worth' => float 654
      protected 'userId' => int 1712

您可以使用 array_columnuserId 作为键,将 worth 作为值。

使用map重复第一个数组。检查key是否存在,如果存在则替换value.

$arr1 = [{"id":1,"value":40},{"id":2,"value":30}];
$arr2 = [{"userId":1,"worth":20},{"userId":2,"worth":10}];

//Use array_column to make the userId as the key and worth as the value.
$arr2 = array_column($arr2, 'worth', 'userId');


//Use `map` to reiterate the first array. Check if the key exist on $arr2, if exist replace the `value`. If not replace it with empty string.
$results = array_map( function($v) use ( $arr2 ) {
    $valueInArr2 = array_search($v->id, array_column($arr2, 'userId'));
    $v->value = $valueInArr2 ? $valueInArr2 : "";
    return $v;
}, $arr1);


echo "<pre>";
print_r( $results);
echo "</pre>";

这将导致:

Array
(
    [0] => Array
        (
            [id] => 1
            [value] => 20
        )

    [1] => Array
        (
            [id] => 2
            [value] => 10
        )

)

更新: 使用对象。我没有测试过这个。

$arr1 = .....;
$arr2 = .....;

//Make the object into array
$arr2 = array_reduce($arr2, function($c,$v) {
    $c[ $v->userId ] = array(
        'worth' => $v->worth;
        'userId' => $v->userId;
    );
    return $c;
},array());

//Update the first array
$results = array_map( function( $v ) use ( $arr2 ) {
    $val = array( 'id' => $v->id );
    $val['value'] = isset( $arr2[ $v->id ] ) ? $arr2[ $v->id ] : "";
    return $v;
}, $arr1);

你可以试试这个方法(使用 laravel 集合):

$arr1 = '[{"id":1,"value":40},{"id":2,"value":30}]';
$arr2 = '[{"userId":1,"worth":20},{"userId":2,"worth":10}]';

$arr1 = json_decode($arr1, true);
$arr2 = json_decode($arr2, true);
$col2 = collect($arr2);

foreach ($arr1 as &$value) {
    $value['value'] = $col2->where('userId', $value['id'])->first()['worth'];
}
echo "<pre>";
print_r($arr1);

输出:

Array
(
[0] => Array
    (
        [id] => 1
        [value] => 20
    )

[1] => Array
    (
        [id] => 2
        [value] => 10
    )
)