0

I made a post in a form converting my javascript localstorage to a post request. From there I tried to decode my json string to make an object in PHP.

How my php code looks before I echo it

    $cart_items = $_POST['cart_items'];
    $cart_items = json_encode($cart_items);
    $array_test = json_decode($cart_items);
    print_r($array_test);

What it returns in browser

[{\"id\":83494890,\"title\":\"2020 Hino 358\",\"partType\":\"Bumpers\",\"price\":100,\"stockNumber\":12313131312,\"thumbImg\":\"/jOIY91KhEby8_f.jpg\",\"permalink\":\"/part-description/?part=83494890\",\"maxQuantity\":1,\"requestedQuantity\":\"3\"}
,{\"id\":83493833,\"title\":\"2009 Freightliner 5020080\",\"partType\":\"ABS Modulator Valves\",\"price\":150,\"stockNumber\":\"P-1211111111157\",\"thumbImg\":\"/OOjQbsi6p8kX_f.jpg\",\"permalink\":\"/part-description/?part=83493833\",\"maxQuantity\":1,\"requestedQuantity\":\"1\"}]

I know that typically when seeing json data there isn't forward slashes everywhere. I tried to json_decode into an array rather than an object, then make a foreach for each object inside. But I got this error returned "Invalid argument supplied for foreach()"

How do I make this json string convert to an array of objects? Thank you

1
  • Could you var_dump($_POST); before that and share here what would be coming in on POST? Commented Aug 31, 2020 at 16:53

2 Answers 2

1

The problem I was having was when I was getting the $_POST[] it was using PHP's "magic quotes" which was giving me improper format for my json. That being said, after disabling this, it removes the slashes.

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

1 Comment

Magic quotes has been obsolete for years. You must be using a very old PHP version.
0

It looks like $_POST['cart_items'] already contains JSON. So you just need to decode it, not encode it first.

$array_test = json_decode($_POST['cart_items'], true);
print_r($array_test);

But it's actually encoded twice, that's why it has escaped quotes, so you need to call json_decode() twice. But it's missing the double quotes around the whole thing, and the embedded newline is not valid.

The following works:

<?php
  $cart_items = '"[{\"id\":83494890,\"title\":\"2020 Hino 358\",\"partType\":\"Bumpers\",\"price\":100,\"stockNumber\":12313131312,\"thumbImg\":\"/jOIY91KhEby8_f.jpg\",\"permalink\":\"/part-description/?part=83494890\",\"maxQuantity\":1,\"requestedQuantity\":\"3\"},{\"id\":83493833,\"title\":\"2009 Freightliner 5020080\",\"partType\":\"ABS Modulator Valves\",\"price\":150,\"stockNumber\":\"P-1211111111157\",\"thumbImg\":\"/OOjQbsi6p8kX_f.jpg\",\"permalink\":\"/part-description/?part=83493833\",\"maxQuantity\":1,\"requestedQuantity\":\"1\"}]"';
  $array_test = json_decode(json_decode($cart_items));
  print_r($array_test);

I suggest you find the code that's sending the cart_item POST parameter and fix it so it doesn't do all this extra encoding.

6 Comments

So I did try this at first, but nothing gets printed without the json_encode. However when I print the string without the decode it still has the slashes and looks very similar.
it's not valid JSON. There needs to be " around it, and it can't have a newline in it.
@IncredibleHat If it were an array of JSON, it would look like ["{\"id\":83494890,...]
Thank you! This worked great. I just needed to get the post type, add quotes around it then do a double json_decode. I don't understand what happened. I know this isn't the best code on my part. I think posting the input reformatted it. Thank you for heading me in the right direction!
@Barmar hmm. Yeah, something aint right with the incoming data. Hopefully OP can figure that out now that they got a better handle on it :D
|

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.