sqlnoob sqlnoob - 2 years ago 166
MySQL Question

Convert string with array of json (PHP)

I stored array of JSON in mysql as a string

[{"id": 1, "mark": 5}, {"id": 2, "mark": 3}, {"id": 3, "mark": 2}]

When I try to use it

@foreach ($places as $place)
@foreach ($place->rates as $rate)



There is a problem.
$rate->marks is a string. How shoul I decode it to array?


of course I tried to use json_decode, but it returns error

htmlentities() expects parameter 1 to be string, array given


I', stupid. Problem was not in json_decode. I tried to print result with {{ }} which needs string as argument.

Answer Source

There are no rate in your json, and also no marks.

Check Online

$json = '[{"id": 1, "mark": 5}, {"id": 2, "mark": 3}, {"id": 3, "mark": 2}]';
$places = json_decode ($json);

foreach ($places as $place)

Design the code for the Laravel, Online Debug does't have that feature.

Let me know.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download