1

I'm sending the following string trough a javascript function into a PHP file:

    function showcart()
{
    document.getElementById('cart').value = JSON.stringify(paypal.minicart.cart.items());
}

On PHP side the string comes as follows:

echo $_POST['cart']; 
[{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010302","item_image":"images/1010302-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":2,"href":"http://teste/index.html#5"},"_options":[],"_discount":100,"_amount":99,"_total":98,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010575","item_image":"images/1010575-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA 1010707","item_image":"images/1010707-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}},{"_data":{"cmd":"_cart","add":"1","item_name":"FILA SCM00514","item_image":"images/scm00514-1.png","amount":99,"discount_amount":"50.00","submit":"Adicionar ao carrinho","quantity":1,"href":"http://teste/index.html#5"},"_options":[],"_discount":50,"_amount":99,"_total":49,"_eventCache":{"change":[[null,null]]}}]

What I'm looking for is to save each field into separate variables, for example:

item = 1 item_name = FILA 1010302, item_image = images/1010302-1.png quantity = 2
item = 2 item_name = FILA 1010575, item_image = images/1010575-1.png quantity = 1

I'm trying several ways using json_decode and html_entity_decode but I just can't reach the desired output.

Can someone help me on this, I'm assuming this is a fairly easy task, but I'm new to PHP, please try to understand.

Thank you in advance!

2
  • There are many item_name, item_image etc. in there. Commented Mar 30, 2020 at 21:40
  • I'm sorry the question was not clear enough, I have edited it. The goal is to get some information according to the position of the array. @AbraCadaver Commented Mar 30, 2020 at 21:52

1 Answer 1

2

Using an online JSON parser, http://json.parser.online.fr/ , I can see that $_POST['cart'] is an array of 5 objects. Within each object there are 6 more objects (_data, _options, _discount, etc.). Then within the _data object there are nine items. In order to get to the values you would need to look at where they are in that nest of JSON objects.

$item_name = json_decode($_POST['cart'], true)[0]["_data"]["item_name"];

$array_of_items_in_cart = json_decode($_POST['cart'], true);
$first_item_in_cart = array_of_items_in_cart[0];
$name_of_first_item_in_cart = first_item_in_cart["_data"]["item_name"];
$first_item_discount = first_item_in_cart["_discount"];
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your advice @Raymond Mutyaba, but isn't this javascript code? What I'm looking for is too decode this on php side. This code on PHP won't give an error?
As pointed out in the answer "json_decode" is the php command to take JSON and convert it to an array or object
Sorry about that. I have been writing in javascript lately. I have edited the code to use PHP. Using true inside json_decode turns $_POST['cart'] into an array, and without true it becomes an object.

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.