Gabriel Meono
Gabriel Meono

Reputation: 1000

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.

Upvotes: 0

Views: 2312

Answers (3)

Toby
Toby

Reputation: 1660

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.

Upvotes: 0

alex
alex

Reputation: 490153

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.

Upvotes: 0

spicykimchi
spicykimchi

Reputation: 1151

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'];

Upvotes: 1

Related Questions