即時通訊對於PHP來說相當新穎,因爲我一直在尋找最近2小時的信息,沒有任何示例似乎有所幫助。 我有什麼我認爲被稱爲嵌套數組?他們兩個人。幾乎我需要能夠匹配來自不同陣列的id。並減少庫存的金額。PHP在foreach中更改數組值
<?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/>';
}
}
}
?>
這就是我的代碼。我認爲這可能會起作用,因爲在退出「if」功能之前,我會給股票賦予新的價值。但我想我錯了
$item['stock'] = $stock_quantity;
任何建議將受到歡迎
EDIT1:什麼即時試圖做的是從$項目陣列ID從$比較ID的訂單。如果它與庫存減去數量匹配並顯示剩餘庫存。希望其更清晰
你能解釋更準確地說你想要做什麼? – Perumal
使用需要傳遞你想改變的數組通過引用http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach – markdwhite
你能特別說出什麼問題嗎? – kerv