When you do your foreach
loop, you're iterating over your array. When you say as $pizza => $price
, the variable $pizza
is assigned the key of the current array item you're looking at and $price
is assigned the value. So in the first iteration of the loop $pizza
is kebabpizza
and $price
is 80
.
Next, you're doing a for
loop over the string kebabpizza
(or whatever the key of the array is at that time). When you try to access $pizza[$i]
in the first iteration of the for
loop you're getting the letter k
in kebabpizza
. You're then echo
ing out k
and then the price of the pizza.
Following that you have a break
statement which will terminate the inner for
loop and the outer foreach
loop will continue. That's why you're seeing only the first letters.
Assuming you want to output the name of the pizza and the price, then you don't need the inner for
loop at all:
$total = 0;
$pizzas = array('kebabpizza' => 80, 'calzone' => 60, 'vesuvio' => 60);
foreach ($pizzas as $pizza => $price) {
echo "$pizza - $price<br>";
$total +=$price;
}
echo "Your order is gonna cost you $total kr<hr>";