簡體   English   中英

從Array1中的特定行值中減去Array2中的行值

[英]Subtract row values in Array2 from specific row values in 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);

然后輸出應該是:

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

根據您的要求,以下內容將滿足您的要求:

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

如果你需要循環一個更大的陣列,那么我所提供的只是更大拼圖的一部分。

您的數組結構與多個記錄看起來略有不同,代碼以丑陋的方式運行。 我假設你在談論這樣的事情:

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

Jesse的答案沒有經過測試,也沒有提供所需的輸出,因為“stock”數組沒有被修改 - 數組的副本正在循環中被修改 - 所以如果你試圖將結果打印到屏幕上,沒有變化。

要通過引用進行修改,請在第一個循環中使用&之前的值變量。

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數組轉換為flattened,product_id-keyed數組以用作查找。

代碼:( 演示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM