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
Post a Comment