I have an associative array (an array filled with arrays), trhat looks like this:
Name: Mike
Color: red
Quantity: 2
Name: Kim
Color: green
Quantity: 5
I want to loop through array until I find the name "Mike" and then modify the quantity by 1. For the life of me I can't figure out how to do this. Any help is greatly appreciated, below is my code:
<?php
$var1 = "mike";
$var2 = 3;
$var3= "red";
$arr= array("cart"=> array($var1,$var2,$var3));
?>
<font color="red">Original array:</font><br>
<?php
echo $arr["cart"][0];
echo "<br>";
print_r ($arr["cart"]);
?>
<p>
<font color="red">Add new info to array:</font><br>
<p>
<font color="red">Loop through array and check if name "Mike" exists in it, if so update quantity by 1:</font><p>
<?php
foreach ($arr as $arrnew) {
if ($arrnew[0]=="mike"){
echo "Mike does exist";
#here is where my problem is
$arr[0][1]=$arrnew[1]+1;
}
}
?>
<p>
<p>
<font color="red">Loop through array, organized by key </font><br>
<?php
foreach ($arr as $arrnewer) {
echo "Name is: $arrnewer[0]<br>";
echo "Quantity: $arrnewer[1]<br>";
}
print_r ($arr);
?>