0

How can i parse a string of an array of json objects in php(more specifically Laravel)? I have a hidden input on a form whose value i set using jquery. The value contains an array of json objects in form of a string that look like this:

"{"id":"1","qty":"2","sp":"140.00"},{"id":"17","qty":"2","sp":"100.00"},{"id":"27","qty":"6","sp":"80.00"},{"id":"22","qty":"6","sp":"60.00"}"

I want to parse this so that i can access the data as actual objects like:

//this gets the first object
$myInput[0];
//this gets the id of the first object
$myInput[0]->id;

1 Answer 1

1

I managed to do what you want by this code (added '[ in the beginning and closing it in the last) :

$s = '[{"id":"1","qty":"2","sp":"140.00"},{"id":"17","qty":"2","sp":"100.00"},{"id":"27","qty":"6","sp":"80.00"},{"id":"22","qty":"6","sp":"60.00"}]';

$d =  json_decode($s);
echo $d[0]->id; // 1
Sign up to request clarification or add additional context in comments.

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.