【问题标题】:htmlentities() expects parameter 1 to be string, array given, Json_decode ;htmlentities() 期望参数 1 是字符串,给定数组, Json_decode ;
【发布时间】:2017-01-11 14:24:08
【问题描述】:

我有问题。我无法在 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

【问题讨论】:

标签: php json laravel


【解决方案1】:

要解决您的第二个问题,请使用 json_decode($value-&gt;working, true) 解析数组中的 json,而不是对象。

我建议你使用以下代码 sn-p。如果比较,代码将解析一次正确的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

【讨论】:

  • 我会测试 :D
  • @Andreyj Ludinovskov:我使用了你的代码,但我仍然看到通知htmlentities() expects parameter 1 to be string, array given at htmlentities(array('Đại Học'), '3', 'UTF-8', false) in helpers.php line 531 我不知道为什么它不起作用:'(
  • @BiHíp 检查 $result[0] != " " 如果它总是数组是否有意义?
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 2017-05-17
  • 2017-09-23
  • 1970-01-01
  • 2016-10-26
相关资源
最近更新 更多