1

I am trying to convert an array where I store some 'products' ($_SESSION['cart']) into a JSON format, but it has been impossible for me

The structure of the $_SESSION['cart'] variable is

$_SESSION['cart']['id_product']['quantity']

I'm using this code to obtain each product, I think i can obtain an array from here

foreach($_SESSION['cart'] as $id_product => $quantity) { 
code
}

Thanks

2 Answers 2

4

Just use the built-in function json_encode:

$myjson = json_encode($_SESSION['cart']);

Substitute whatever variable you want, e.g., $_SESSION['cart']['id_product']['quantity'].

Sign up to request clarification or add additional context in comments.

Comments

1
   <?php
      session_start();
      $_SESSION['cart'] = array('test' => 'stuff');
      echo json_encode($_SESSION); //Prints {"cart":{"test":"stuff"}}
   ?>

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.