简体   繁体   English

PHP在foreach中更改数组值

[英]PHP changing array value in foreach

im quite new to php and as i was looking for info for last 2 hours, no examples seemed to help. 我对php很陌生,因为我过去2个小时一直在寻找信息,所以似乎没有帮助的例子。 i have what i think is called nested array? 我有所谓的嵌套数组吗? two of them. 他们两个人。 pretty much i need to be able to match id from different arrays. 我几乎需要能够匹配来自不同数组的ID。 and detract the amount from stock. 并从库存中扣除金额。

<?php
$items = array(
array('id' => 34, 'name' => 'Kompiuterius ASUS ASX89', 'price' => 639.00, 'stock' => 3),
array('id' => 1008, 'name' => 'Monitorius AOC 27IPS', 'price' => 223.00, 'stock' => 7),
array('id' => 965, 'name' => 'Tracer kilimėlis pelytei', 'price' => 2.00, 'stock' => 20),
array('id' => 567, 'name' => 'Pelytė Logitech A52', 'price' => 16.00, 'stock' => 14),
array('id' => 1123, 'name' => 'Klaviatūra Razer Chroma 2016', 'price' => 109.00, 'stock' => 6)
);
$orders = array(
array('purchase_date' => '2016-11-12', 'item_id' => 34, 'quantity' => 1),
array('purchase_date' => '2016-11-12', 'item_id' => 1008, 'quantity' => 2),
array('purchase_date' => '2016-11-13', 'item_id' => 965, 'quantity' => 1),
array('purchase_date' => '2016-11-15', 'item_id' => 1123, 'quantity' => 4),
array('purchase_date' => '2016-11-11', 'item_id' => 34, 'quantity' => 2)
);

foreach ($orders as $order){
  $purchase_id = $order['item_id'];
  $purchase_quantity = $order['quantity'];
  foreach ($items as $item){
    $stock_id = $item['id'];
    $stock_quantity = $item['stock'];
    if ($purchase_id == $stock_id){
      $stock_quantity = $stock_quantity - $purchase_quantity;
      $item['stock'] = $stock_quantity; //something with &?
      echo 'Prekiu, pazymetu numeriu ' . $stock_id . ' liko: ' . $stock_quantity . ' vnt. ' . '<br/>';
    }
  }
}
?>

thats pretty much my code. 多数民众赞成在我的代码。 i thought this might work, as with following line i would give new value to stock before exiting "if" function. 我认为这可能可行,因为在退出“ if”功能之前,我会在下一行中给股票赋予新的价值。 but i guess im wrong 但我想我错了

$item['stock'] = $stock_quantity;

any suggestions would be welcome 欢迎大家提出意见

EDIT1: what im trying to do is compare id from $items array with id from $orders. EDIT1:我想做的是比较$ items数组中的id与$ orders中的id。 if it matches from stock subtract quantity and display the remaining stock. 如果它与库存减去数量匹配,则显示剩余库存。 hope its more clear 希望它更清楚

If you change foreach ($items as $item) to foreach ($items as &$item) it will change $item. 如果将foreach ($items as $item)更改为foreach ($items as &$item) ,它将更改$ item。 Adding a & before a variable will make it a reference and not a new object. 在变量前添加&将使它成为引用而不是新对象。 This way when you change item it will change the original item. 这样,当您更改项目时,它将更改原始项目。

A foreach loop works by copying each value into a temporary variable. foreach循环通过将每个值复制到一个临时变量中来工作。

If you want to edit the original array, you have two solutions : 如果要编辑原始数组,则有两种解决方案:

Either pass the value with a reference, using & : 使用&传递带有参考的值:

foreach ($items as &$item) {
    /*...*/
    $item['stock'] = $stock_quantity;
}

Or use the $key=>$value notation and edit the original array : 或者使用$key=>$value表示法并编辑原始数组:

foreach ($items as $key => $item) {
    /*...*/
    $items[$key]['stock'] = $stock_quantity;
}
$items = array(
    array('id' => 34, 'name' => 'Kompiuterius ASUS ASX89', 'price' => 639.00, 'stock' => 3),
    array('id' => 1008, 'name' => 'Monitorius AOC 27IPS', 'price' => 223.00, 'stock' => 7),
    array('id' => 965, 'name' => 'Tracer kilimėlis pelytei', 'price' => 2.00, 'stock' => 20),
    array('id' => 567, 'name' => 'Pelytė Logitech A52', 'price' => 16.00, 'stock' => 14),
    array('id' => 1123, 'name' => 'Klaviatūra Razer Chroma 2016', 'price' => 109.00, 'stock' => 6)
);
$orders = array(
    array('purchase_date' => '2016-11-12', 'item_id' => 34, 'quantity' => 1),
    array('purchase_date' => '2016-11-12', 'item_id' => 1008, 'quantity' => 2),
    array('purchase_date' => '2016-11-13', 'item_id' => 965, 'quantity' => 1),
    array('purchase_date' => '2016-11-15', 'item_id' => 1123, 'quantity' => 4),
    array('purchase_date' => '2016-11-11', 'item_id' => 34, 'quantity' => 2)
);

foreach ($orders as $order){
    $purchase_id = $order['item_id'];
    $purchase_quantity = $order['quantity'];

    foreach ($items as &$item){
        $stock_id = $item['id'];
        $stock_quantity = $item['stock'];

        if ($purchase_id == $stock_id) {
            $stock_quantity = $stock_quantity - $purchase_quantity;
            $item['stock'] = $stock_quantity;
            echo 'Prekiu, pazymetu numeriu ' . $stock_id . ' liko: ' . $stock_quantity . ' vnt. ' . '<br/>';
        }
        unset($item);
    }
}

Make sure you add reference & before the foreach 's temporary variable if you need to manipulate the current element. 如果需要操纵当前元素,请确保在foreach的临时变量之前添加引用&

Once you're done with the foreach loop, remove the reference of the temporary variable using unset() function. 一旦完成了foreach循环,请使用unset()函数删除临时变量的引用。

Therefore the variable will be destroyed otherwise it will still be accessible outside of foreach since it's still a reference variable. 因此,该变量将被销毁,否则它仍将是引用变量,因此仍然可以在foreach之外访问。

For more information about this, you could refer to the following post: 有关此的更多信息,您可以参考以下文章:

Strange behavior Of foreach foreach的奇怪行为

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

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