简体   繁体   中英

How to check and replace array if array value exists in another multidimensional array

I have two arrays and I want to compare array based on day_of_week and status value.

If day_of_week is existing in another array and status is 1 then replace the array with matched array.

$array1

Array
(
    [0] => Array
        (
            [id] => 4
            [day_of_week] => sun
            [description] => sun test description
            [status] => 0
            [date] => 2019-01-11
        )

    [1] => Array
        (
            [id] => 5
            [day_of_week] => mon
            [description] => mon test description
            [status] => 0
            [date] => 2019-01-11
        )
)

$array2

Array
(
    [0] => Array
        (
            [id] => 1
            [day_of_week] => mon
            [description] => mon updated test description
            [status] => 1
            [date] => 2019-01-11
        )
)

Desired Output:

Array
(
    [0] => Array
        (
            [id] => 4
            [day_of_week] => sun
            [description] => sun test description
            [status] => 0
            [date] => 2019-01-11
        )

    [1] => Array
        (
            [id] => 1
            [day_of_week] => mon
            [description] => mon updated test description
            [status] => 1
            [date] => 2019-01-11
        )
)

I have tried with array_intersect function but not found solution to compare value based on day_of_week and status . Can anyone please help me?

$array = array_intersect($array1, $array2);

It seems can be done by a loop

$array2 = array_column($array2, null, 'day_of_week');
foreach ($array1 as $k=>$x) {
  if (isset($array2[$x['day_of_week']]) and $array2[$x['day_of_week']]['status'] == 1) {
      $array1[$k] = $array2[$x['day_of_week']];
  }
}

demo

As day of week is unique in the arrays, you can use array_column() to index both arrays by this and the use array_replace() to replace the data from the second array into the first array.

Also (thanks to splash58) you need filter the second array to only leave ones with status = 1...

$array2 = array_filter($array2, function($entry) { return $entry['status'] == '1'; });
$array1 = array_column($array1, null, "day_of_week");
$array2 = array_column($array2, null, "day_of_week");
$array1 = array_replace($array1, $array2);

print_r(array_values($array1));

using array_values() at the end removes the index in case you don't need it.

You can use array_map to process each element of $array1 and check if it's corresponding entry in $array2 (located using array_search on the day_of_week column) has status == 1 . If so, return the element from $array2 , otherwise keep the value from $array1 :

$array1 = array_map(function ($v) use ($array2) {
    if (($k = array_search($v['day_of_week'], array_column($array2, 'day_of_week'))) !== false && $array2[$k]['status'] == 1)
        return $array2[$k];
    else
        return $v;
}, $array1);
print_r($array1);

Output:

Array ( 
    [0] => Array ( [id] => 4 [day_of_week] => sun [description] => suntestdescription [status] => 0 [date] => 2019-01-11 )
    [1] => Array ( [id] => 1 [day_of_week] => mon [description] => monupdatedtestdescription [status] => 1 [date] => 2019-01-11 ) 
)

Demo on 3v4l.org

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