比较数组并根据结果执行代码


Comparing arrays and execute code depending on result

我似乎找不到在PHP中使用数组来实现这一点的方法。

$id_qty1 = array(1 => 20, 2 => 30, 3 => 40); 
$id_qty2 = array(1 => 30, 2 => 20, 3 => 50); 

我想检查一个阵列与另一个阵列,并:

如果相同的密钥和更大的值做一件事

如果相同的键和较小的值做另一件事

这样的东西可以做到:

foreach ($id_qty1 as $key => $value) {
    if (isset($id_qty2[$key])) {
        if ($id_qty2[$key] > $value) {
            // do one thing
        } else {
            // do another thing 
        }
    }
}