简体   繁体   English

从Array1中的特定行值中减去Array2中的行值

[英]Subtract row values in Array2 from specific row values in Array1

I would like to subtract the quantity of $array2 from the stocks of $array1 . 我想从$array1stocks中减去$array2quantity

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

And then the output should be: 然后输出应该是:

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

With what you have given the following will do what you are asking for: 根据您的要求,以下内容将满足您的要求:

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

If you need to loop through a bigger array then what I have given is only part of the larger puzzle. 如果你需要循环一个更大的阵列,那么我所提供的只是更大拼图的一部分。

Your array structure looks slightly different with multiple records, the code works out like this in an ugly manner. 您的数组结构与多个记录看起来略有不同,代码以丑陋的方式运行。 I'm assuming you're talking about something like this: 我假设你在谈论这样的事情:

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

...It's a multi-dimensional array (typical for records pulled from a database). ...这是一个多维数组(通常用于从数据库中提取的记录)。

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]);
        }
    }}

Jesse's answer wasn't tested and will not provide the desired output because the "stocks" array wasn't being modified -- a copy of the array was being modified in the loop -- so if you try to print the result to screen, there would be no change. Jesse的答案没有经过测试,也没有提供所需的输出,因为“stock”数组没有被修改 - 数组的副本正在循环中被修改 - 所以如果你试图将结果打印到屏幕上,没有变化。

To modify by reference, use & just before the value variable in the first loop. 要通过引用进行修改,请在第一个循环中使用&之前的值变量。

Also the unset() key must come from the inner loop to be accurate. unset()键也必须来自内循环才能准确。

Additionally, if the "sales" "product_id"s are unique, then breaking the inner loop upon matching will improve performance. 另外,如果“sales”“product_id”是唯一的,那么在匹配时打破内部循环将提高性能。 (This is how array_search() works.) (这就是array_search()工作原理。)

Code: ( Demo ) 代码:( 演示

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

Output: 输出:

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,
  ),
)

Alternatively, you can converted the sales array into a flattened, product_id-keyed array to serve as a lookup. 或者,您可以将sales数组转换为flattened,product_id-keyed数组以用作查找。

Code: ( Demo ) 代码:( 演示

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

Output: 输出:

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,
  ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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