1

im quite new to php and as i was looking for info for last 2 hours, no examples seemed to help. 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. 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. 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. if it matches from stock subtract quantity and display the remaining stock. hope its more clear

NulisDefo
  • 325
  • 2
  • 7
  • 19

3 Answers3

7

A foreach loop works by copying each value into a temporary variable.

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 :

foreach ($items as $key => $item) {
    /*...*/
    $items[$key]['stock'] = $stock_quantity;
}
roberto06
  • 3,844
  • 1
  • 18
  • 29
  • thanks. adding & worked. the interesting thing is (as u see my comment in code) i marked about & and ried it before, but ti didnt work. and now after reading this i treid again and now it does. – NulisDefo Dec 01 '16 at 15:09
  • Yes, I saw your comment, but I guess you didn't add `& ` in the right place. In my opinion, using the second option is better (though longer to write), because, using `&`, the last reference will still be reachable after the `foreach` loop is closed. [Read here](http://php.net/manual/en/control-structures.foreach.php). – roberto06 Dec 01 '16 at 15:12
  • im sry, but i dont understand what u mean by this comment, could u explain? – NulisDefo Dec 01 '16 at 15:14
  • because i used this code below what u see posted in question and now the id=34 post doesnt duplicate. 'code' function likutis($left_id, $left_stock){ echo 'Prekes, pazymetos numeriu ' . $left_id . ' liko: ' . $left_stock . ' vnt. ' . '
    '; } foreach ($items as $likutis){ $left_id = $likutis['id']; $left_stock = $likutis['stock']; likutis($left_id, $left_stock); } sry, no idea how to make it look like code in comments
    – NulisDefo Dec 01 '16 at 15:17
  • If you use `foreach ($items as &$item) {}`, then, once the loop is closed, `$item` will still exist (as a reference to the last element of `$items`) and that could cause some problems if you're not careful. If you use `foreach ($items as $key => $item) {}`, then, once the loop is closed, `$item` won't exist anymore. I hope that's clear enough `;)` – roberto06 Dec 01 '16 at 15:17
  • used ur second option. will try to remember that. thank u for advice – NulisDefo Dec 01 '16 at 15:22
2

If you change foreach ($items as $item) to foreach ($items as &$item) it will change $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.

Bart Bergmans
  • 4,061
  • 3
  • 28
  • 56
  • thanks. adding & worked. the interesting thing is (as u see my comment in code) i marked about & and ried it before, but ti didnt work. and now after reading this i treid again and now it does. – NulisDefo Dec 01 '16 at 15:09
  • Maybe you put it on same line as the comment instead of in the foreach? – Bart Bergmans Dec 01 '16 at 15:11
  • 1
    i dont know. maybe i put it on first foreach instead of second? thank you a lot anyway :) – NulisDefo Dec 01 '16 at 15:13
1
$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.

Once you're done with the foreach loop, remove the reference of the temporary variable using unset() function.

Therefore the variable will be destroyed otherwise it will still be accessible outside of foreach since it's still a reference variable.

For more information about this, you could refer to the following post:

Strange behavior Of foreach

Community
  • 1
  • 1
Wolverine
  • 1,712
  • 1
  • 15
  • 18