php foreach loop only display the first letter of array -


$item= "stackoverflow"; $price = "30.00";  $cs1_array = array(); $cs1_array['item'] = $item; $cs1_array['price'] = $price;      if($cs1_array > 0){             echo "<table>";             foreach ($cs1_array $item){                 echo "<tr>";                 echo "<td>{$item['item']}</td><td>{$item['price']}</td>";                 echo "</tr>";             }             echo "</table>";         } 

this output first alphabet of array only. example display letter s of stackoverflow. want display whole stackoverflow word, how so?

you did create single level array, while need multidimensional array (i.e. array in array):

$cs1_array = array(); $cs1_array[] = array(     'item' => $item,     'price' => $price ); 

compare this:

// wrong array(2) {   ["item"]=>   string(13) "stackoverflow"   ["price"]=>   string(5) "30.00" } 

vs.

// right array(1) {   [0]=>   array(2) {     ["item"]=>     string(13) "stackoverflow"     ["price"]=>     string(5) "30.00"   } } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -