Laravel,得到嵌套数组

时间:2018-02-26 14:44:51

标签: php laravel

所以,这是我的json格式数据

{
   "New Purchase Orders Created":{
      "date":"2018-02-26 14:05:14.000000",
      "timezone_type":3,
      "timezone":"UTC"
   },
   "colour":"bg-primary"
}

这是我在blade.php中的观点

<ul class="list-group list-group-flush">
         @foreach( $notifications as $notification )
                <?php $notificationData = json_decode($notification->data, true);?>
         @foreach( $notificationData as $key=>$item )
                <li class="list-group-item text-white {{ $notificationData['colour'] }}">
                  @if ($key!='colour')
                      {{ $key }}<br>  
                      {{ $item['date'] }}

                  @endif
                </li>
          @endforeach
          @endforeach
</ul>

我想将数据输入刀片。那我该怎么办?

enter image description here

这就是我想要的。

请帮忙。感谢。

2 个答案:

答案 0 :(得分:0)

使用json_decode。这适用于您已经展示的JSON:

{{ json_decode($json, true)['New Purchase Orders Created']['date'] }}

如果每个JSON中的New Purchase Orders Created键可能不同,请使用array_first()帮助器:

{{ array_first(json_decode($json, true))['date'] }}

如果您有许多元素,请迭代它们:

$array = json_decode($json, true);
@foreach ($array as $key => $element)
    {{ $array[$key]['date'] }}
@endforeach

答案 1 :(得分:0)

假设上面的JSON是一个像这样的对象数组:

<?php
$notifications = '[{
   "New Purchase Orders Created":{
      "date":"2018-02-26 14:05:14.000000",
      "timezone_type":3,
      "timezone":"UTC"
   },
   "colour":"bg-primary"
},
{
   "New Purchase Orders Created":{
      "date":"2018-02-26 14:05:14.000000",
      "timezone_type":3,
      "timezone":"UTC"
   },
   "colour":"bg-primary"
}]';
?>

因此,脚本将是这样的:

<ul class="list-group list-group-flush">
    @foreach( json_decode($notifications, true) as $notification => $data )
        <?php $item = $data[key($data)]; ?>
        <li class="list-group-item text-white {{ $data['colour'] }}">
            {{ key($data) }}<br>  
            {{ $item['date'] }}
        </li>
    @endforeach
</ul>