简体   繁体   中英

Replacing values in an array basing on values from another

Having the 2 following arrays:

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

And:

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

What I want to have in the end, is the following result:

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

So here, I want to replace in the first array, the values with those in the second array, according to the ìd`. I made something like that:

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

It is working for those given arrays, but if you have arrays with more than 1 million data, it will never be done ! The question is, if there are some PHP functions which can do this hard work?

Update

The arrays format is updated and now I should use the following format:

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

And:

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

Result of Var_dump:

Array 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

Array 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

You can use array_column to make the userId as the key and worth as the value.

Use map to reiterate the first array. Check if the key exist, if exist replace the 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>";

This will result to:

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

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

)

Update: Using Object. I have not tested this.

$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);

you can try this way (using laravel collection):

$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);

Output:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM