4

我想从of中减去quantityof 。$array2stocks$array1

$array1= ([product_id]=>4, [stocks]=>20)

$array2= ([product_id]=>4, [quantity]=>3)

So that would be:
$array1= ([0]=> 4, [1] => 20);
$array2= ([0]=> 4, [1] => 3);

然后输出应该是:

$array1= ([0]=> 4, [1] => 17);
4

3 回答 3

2

使用您所提供的内容,以下内容将满足您的要求:

if($array1['product_id'] == $array2['product_id']) {
    $array1['stocks'] -= $array2['quantity'];
}

如果您需要遍历更大的数组,那么我给出的只是更大难题的一部分。

于 2013-10-22T19:08:25.310 回答
2

您的数组结构看起来与多条记录略有不同,代码以丑陋的方式运行。我假设你在谈论这样的事情:

$array1 = array(
    0=>array('product_id'=>4, 'stocks'=>20), 
    1=>array('product_id'=>5, 'stocks'=>60));
$array2 = array(
    0=>array('product_id'=>4, 'quantity'=>3)
    1=>array('product_id'=>5, 'quantity'=>30));

...这是一个多维数组(通常用于从数据库中提取的记录)。

foreach($array1 as $key=>$value){
    foreach($array2 as $key2=>$value2) {
        if($value['product_id']==$value2['product_id']){
            $value['stocks'] -= $value2['quantity'];
            //optimization to avoid searching this again.
            unset($array2[$key]);
        }
    }}
于 2013-10-22T20:18:50.007 回答
1

杰西的答案没有经过测试,也不会提供所需的输出,因为“股票”数组没有被修改——数组的副本正在循环中被修改——所以如果你尝试将结果打印到屏幕上,不会有任何变化。

要通过引用进行修改,&请在第一个循环中的 value 变量之前使用。

此外,unset()密钥必须来自内部循环才能准确。

此外,如果“sales”“product_id”是唯一的,那么在匹配时打破内部循环将提高性能。(这就是array_search()工作原理。)

代码:(演示

$stocks = [
    ['product_id'=>2, 'stocks'=>50], 
    ['product_id'=>3, 'stocks'=>100],
    ['product_id'=>4, 'stocks'=>20], 
    ['product_id'=>5, 'stocks'=>60]
];
$sales = [
    ['product_id'=>4, 'quantity'=>3],
    ['product_id'=>5, 'quantity'=>30]
];

foreach ($stocks as &$row) {                             // modify by reference
    foreach ($sales as $k => $row2) {                    // search for product_id match
        if ($row['product_id'] == $row2['product_id']) {
            $row['stocks'] -= $row2['quantity'];         // subtract
            unset($sales[$k]);                           // eliminate match from lookup array
            break;                                       // assuming $sales['product_id'] values are unique
        }
    }
}

var_export($stocks);

输出:

array (
  0 => 
  array (
    'product_id' => 2,
    'stocks' => 50,
  ),
  1 => 
  array (
    'product_id' => 3,
    'stocks' => 100,
  ),
  2 => 
  array (
    'product_id' => 4,
    'stocks' => 17,
  ),
  3 => 
  array (
    'product_id' => 5,
    'stocks' => 30,
  ),
)

或者,您可以将 sales 数组转换为扁平化的 product_id 键数组以用作查找。

代码:(演示

$keyed = array_column($sales, 'quantity', 'product_id');
var_export($keyed);
echo "\n---\n";

foreach ($stocks as &$row) {                          // modify by reference
    if (isset($keyed[$row['product_id']])) {          // search for product_id match
        $row['stocks'] -= $keyed[$row['product_id']]; // subtract
    }
}

var_export($stocks);

输出:

array (
  4 => 3,
  5 => 30,
)
---
array (
  0 => 
  array (
    'product_id' => 2,
    'stocks' => 50,
  ),
  1 => 
  array (
    'product_id' => 3,
    'stocks' => 100,
  ),
  2 => 
  array (
    'product_id' => 4,
    'stocks' => 17,
  ),
  3 => 
  array (
    'product_id' => 5,
    'stocks' => 30,
  ),
)
于 2018-05-23T18:50:00.317 回答