Reputation: 487
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;
Upvotes: 0
Views: 741
Reputation: 1935
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
Upvotes: 1