htmlentities()期望参数1为字符串,给定数组,Json_decode;

时间:2016-09-03 17:59:13

标签: php json laravel

我有问题。我无法在foreach中打印$value->working类型unicode的值,但这不起作用,我有通知htmlentities() expects parameter 1 to be string, array given 我正在使用Json。当我回复dd($levelResult)然后

array:1 [▼
  0 => "Đại Học"
]

如果我回复$levelResult[0],那么我会收到通知Invalid argument supplied for foreach()

@foreach( $search as $key => $value)
    @if(json_decode($value->working,JSON_BIGINT_AS_STRING)[0] != " ")           
         @foreach(json_decode($value->working) as $key => $levelResult)
               <p>{{ $levelResult }} </p>               
         @endforeach
    @else
      {{ " " }}
    @endif
@endforeach

1 个答案:

答案 0 :(得分:0)

要解决第二个问题,请使用json_decode($value->working, true)解析数组中的json但不能解析对象。

我建议您使用以下代码段。如果进行比较,代码将解析json一次并正确。

@foreach( $search as $key => $value)
    <?php $result = json_decode($value->working, true, 512, JSON_BIGINT_AS_STRING); ?>
    @if($result[0] != " ")
        @foreach($result as $key => $levelResult)
                   <p>{{ $levelResult[0] }} </p>
        @endforeach
    @else
      {{ " " }}
    @endif
@endforeach