比较php中的2个数组,并更新现有的数组值


compare 2 arrays in php and update the existing array value

我有一个动态数组$fullplan,包含详细信息sno、name、qty1、qty2;另一个名为$centerD的数组(动态数组),具有详细信息sno,name,qty2;

我想比较两个数组中的sno、name,并将$fullplanqty2值与数组$centerD中的corrosponding qty2进行更新。

使用foreach比较两个数组并更新$fullplan数组很容易。

foreach($fullplan as $key => $value)
{
    foreach($centerD  as $key1 => $value1)
    {
        if($value['sno'] == $value1['sno'] && $value['name'] == $value1['name'])
        {
            $fullplan[$key]['qty2'] =  $value1['qty2'];
        }
    }
}
echo "<pre>";
print_r($fullplan);

考虑以下是您的数组。

  $fullplan = array(
        'sno' => '1',
        'name' => 'jack',
        'qty1' => '20');

您可以通过$fullplan['sno]$fullplan['name]访问sno和name密钥。因此,像这样的if($fullplan['sno'] == $centerD['sno'] && $fullplan['name'] == $centerD['name']){ $fullplan['qty2'] = $centerD['qty2']}很容易进行比较

然后可以分配新值,如$fullplan['qty2'] = "your value"