arrays - PHP: Foreach echo not showing correctly -


the output should this:

1. yougurt 4 units price 2000 crc 

but i'm getting this:

item. y y unitsyquantity. 3 3 units3code. s s unitssprice. units 

this script:

    <?php  session_start();  //getting list $list[]= $_session['list'];   //stock $products = array(        'pineaple' => 500, 'banana' => 50, 'mango' => 150,        'milk' => 500, 'coffe' => 1200, 'butter' => 300,       'bread' => 450, 'juice' => 780, 'peanuts' => 800,       'yogurt' => 450, 'beer' => 550, 'wine' => 2500,   );  //saving stuff $_session['list'] = array(     'item' => ($_post['product']),      'quantity' => ($_post['quantity']),     'code' => ($_post['code']), );  //price $price = $products[($_session['list']['item'])] * $_session['list']['quantity'];  $_session['list']['price'] = $price;   //listing echo  "<b>shoppign list</b></br>";  foreach($_session['list'] $key => $item)  {     echo $key, '. ', $item['item'], ' ', $item['quantity'], ' units', $item['price']; }  //recycling list $_session['list'] = $list;  echo "</br> <a href='index.html'>return index</a> </br>";   //printing session print_r($_session);  ?> 

the problem nested 1 level deeper in arrays think are. make clear, $_session may lik (just before entering foreach):

array(1) {       ["list"] => array(3) {            ["item"] => string(8) "pineaple"            ["quantity"] => int(30)            ["price"] => int(15000)      }  }  

(you can use var_dump($var) or print_r($var) methods see value: http://php.net/manual/en/function.var-dump.php http://php.net/manual/en/function.print-r.php)

when iterating on $_session["list"], pass loop 3 times. in first iteration, $key "item", $value "pineaple".

echo $key, '. ', $item['item'], ' ', $item['quantity'], ' units', $item['price'];     "item   .    p                   p                    units   <empty>" 

why? string "item" obvious, it's printed out.

$item['item'] -> 'item' cast (int)0, first character of $item (pineaple) printed: p (the examples of string->int conversion rules example here: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion)

$item['quantity'] -> same above

$item['price'] -> since price higher length of string, empty string printed: $myvar = "hi"; echo $myvar[12234]; // prints empty string

in every iteration output, first word changing. put echo "<br />" @ end of iteration , see it.

i hope helps bit.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -