-3

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)

      {{var_dump($rate->marks)}}

   @endforeach
@endforeach

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

UPD

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

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

SOLVED

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

4
  • First decode the json string and then use it in foreach loop. Commented May 28, 2016 at 10:23
  • use json_decode($rate->marks); Commented May 28, 2016 at 10:24
  • @splash58 of course I tried to use json_decode, but it returns error htmlentities() expects parameter 1 to be string, array given Commented May 28, 2016 at 10:25
  • Possible duplicate of Convert a string to JSON object php Commented May 28, 2016 at 10:25

3 Answers 3

0

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)
      var_dump($place->mark);

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

Let me know.

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

Comments

0

You need to json_decode your marks and then iterate them as array. Something like this (I don't know if it works in Laravel but you can get an idea):

@foreach ($places as $place)
   @foreach ($place->rates as $rate)
      @foreach (json_decode($rate->marks) as $mark)
        {{var_dump($mark->mark)}}
      @endforeach
   @endforeach
@endforeach

Comments

0
<?php
$str = '[{"id": 1, "mark": 5}, {"id": 2, "mark": 3}, {"id": 3, "mark": 2}]';

$places = json_decode($str);

foreach ($places as $place) {
    echo 'id=>' . $place->id;
    echo 'mark=>' . $place->mark;
    echo "<br>";
}

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.