I want to update the product inventory would be glad if you help I could not. My product list array
$_SESSION['item'];
Array (
[0] => Array ( [product_id] => 9 [stock] => 20 )
[1] => Array ( [product_id] => 8 [stock] => 30 )
[2] => Array ( [product_id] => 7 [stock] => 26 )
[3] => Array ( [product_id] => 6 [stock] => 42 )
)
I do
$product_id = mysql_real_escape_string($_POST['product_id']);
$stock = mysql_real_escape_string($_POST['stock']);
$found = false;
foreach($_SESSION['item'] as $product){
if($product_id == $product['product_id']) {
$found = true;
break;
}
}
if($found){
$_SESSION['item'][$product_id]['stock'] += $stock;
}else{
// go get new product and add to $_SESSION['item']
}
$foundafter the foreach loop?stockis not a key ofproduct_idi would restructure the array using the product_id as the first level key$_SESSION['item']is NOT indexed by product_id!