1

I have this JSON that I send through a POST to my PHP file Products.php that is supposed to get the values from the cart ( shipped, name, address, etc) and the values of lines and each product inside ( id, name, category, etc).

{
    "cart": {
        "lines": [{
            "product": {
                "id": 1,
                "name": "Product 1",
                "category": "Category 1",
                "description": "Product 1 (Category 1)",
                "price": 100
            },
            "quantity": 1
        }],
        "itemCount": 1,
        "cartPrice": 100
    },
    "shipped": false,
    "name": "Joe Smith",
    "address": "123 Main Street",
    "city": "Smallville",
    "state": "NY",
    "zip": "10036",
    "country": "USA"
}

In the products.php I decode the JSON:

$jsondata = file_get_contents('php://input');
$data = json_decode($jsondata, true);

Then I print it out like this:

foreach($data as $key => $value) {
    print_r($key  . " => " . $value . "<br>");
  }

And I get all the values but the cart value because it is an array, and I don't know how to access that array and it's values.

Notice: Array to string conversion.

cart => Array
shipped=>
name=> 123
address=> 123
city=> 123
state=> 123
zip=> 123
country=> 123

I need the product ID for each line so that I can insert it in my database when the user makes a new order but I can't seem to be able to access the values nested inside the array, If anyone could help me It would be highly appriciated, thank you very much for taking the time to read this and helping a newbie out.

0 Answers0