PHP: How to echo strings from an Array?
I'm stuck trying to echo strings from an array, all I get is "Array" as text.
This is the array:
$_SESSION['lista'][] = array(
'articulo' => $articulo,
'precio' => $precio,
'cantidad' => 开发者_如何转开发$cantidad);
This is the echo:
echo "1. ".$_SESSION['lista'][0][0]." ".$_SESSION['lista'][0][1]." unidades".", ".$_SESSION['lista'][0][2]." CRC.";
The current output is:
1. Array Array unidades, Array CRC.
Remove [] so it looks like these And put session_start() at starting line;
<?php
session_start();
$_SESSION['lista'] = array(
'articulo' => $articulo,
'precio' => $precio,
'cantidad' => $cantidad);
?>
To access the array:
echo $_SESSION['lista']['articulo'];
echo $_SESSION['lista']['precio'];
You can't access an associative array member with a numerical key as an offset.
Try this...
echo $_SESSION['lista'][0]['articulo'];
An array
's to string type method is called (and returns Array
) when you try to implicitly convert it to a string, e.g. with echo
.
Take a look at print_r along with var_dump etc. As stated in the manual, these functions print the contents of arrays/objects in human readable format.
精彩评论