Error traversing a multidimensional array within a php session

1

how are you? I have a question, I made a session, to contain an array and then use it whenever I want in several parts of my page. the question, is that wanting to bring the data of each array within this session, throws me errors ... with the varDump () there is no drama, the issue is that I must return that data one by one to a jscript. p>

function CargarEnCarro($id){

session_start();
$vuelta = "hola";
$producto[]=[$id, $vuelta];
$_SESSION['CarroCompra'][]= $producto;
//var_dump($_SESSION['CarroCompra']);




 for ($i=0; $i <count($_SESSION['CarroCompra']) ; $i++) { 

     for ($j=0; $j < count($_SESSION['CarroCompra'][$i]) ; $j++) { 
         $valor=$_SESSION['CarroCompra'][$i][$j];
         echo $valor;
     }

}


}

The issue is that I want to test it by showing it in the console, and it gives me the following error, just in the echo line:

<br />
<b>Notice</b>:  Array to string conversion in 
........ on line <b>494</b><br />
Array<br />
    
asked by GALS 23.09.2018 в 18:45
source

3 answers

1

You're making a mess of saving the values in your $_SESSION variable, which works like an array.

For example, you can save the information with a key that identifies it. Apparently what you want is to save within $_SESSION other array (s) with two elements.

A cleaner and more organized solution would be:

  • Declare $producto as an array
  • Within that array, save two elements, one will be identified by the key id and the other will be identified by the key texto :
  • Then you add that array ( $producto ) to your session variable. You can add as many arrays as you need ...
  • Then you read the contents of the session variable with a simple foreach , searching for each element by its key.
  • If the keys were disparate, but the array keeps the same structure, you can read the information with a foreach nested, as explained in the section Particular case below.

As explained, the code would look like this:

$producto=array();
$vuelta = "hola";
$id=1;

$producto['id']=$id;
$producto['texto']=$vuelta;
$_SESSION['CarroCompra'][]= $producto;


foreach ($_SESSION['CarroCompra'] as $row)
{
    echo "id: {$row['id']} - texto: {$row['texto']}".PHP_EOL;
}

The output on the screen is as follows:

id: 1 - texto: hola

Special case:

If the array for any reason should have different keys, for example, let's imagine that it is filled like this:

$producto=array();
$vuelta = "hola";
$id=1;

$producto['id']=$id;
$producto['texto']=$vuelta;

$vendedor=array();
$vendedor['codigo']="Z-001";
$vendedor['nombre']="Zaqueo";

$_SESSION['CarroCompra'][]= $producto;
$_SESSION['CarroCompra'][]= $vendedor;

You can read like this:

foreach ($_SESSION['CarroCompra'] as $subarray)
{
    if (is_array($subarray))
    {
        foreach ($subarray as $k=>$v)
        {
            echo "$k:  $v ".PHP_EOL;
        }
        echo PHP_EOL;   
    }
}

In this case the output will be similar to this:

id:  1 
texto:  hola 

codigo:  Z-001 
nombre:  Zaqueo 
    
answered by 23.09.2018 / 20:45
source
0

I think you should put echo $ value [0] to get the $ id and echo $ value [1] to get $ return

    
answered by 23.09.2018 в 19:26
0

I solved it like that, I do not know if it's the right thing to do, but it works ... let's see how it goes:

  for ($i=0; $i <count($_SESSION['CarroCompra']) ; $i++) { 

      for ($j=0; $j <count($_SESSION['CarroCompra'][$i]) ; $j++) { 

          for ($k=0; $k < count($_SESSION['CarroCompra'][$i][$j]); $k++) { 
            echo ($_SESSION['CarroCompra'][$i][$j][$k]) . ' ';
        }
   }

}
    
answered by 23.09.2018 в 19:40