0

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.

3 Answers 3

1

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the extra [] was the cause.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.