简体   繁体   English

Laravel,获取嵌套数组

[英]Laravel, get nested array

So, this is my json format data 所以,这是我的json格式数据

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

This is my view in blade.php 这是我在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>

I want to get the data into the blade. 我想将数据放入刀片。 So what should I do? 所以我该怎么做?

在此处输入图片说明

This is what i want to get. 这就是我想要的。

Please help. 请帮忙。 Thanks. 谢谢。

Use json_decode . 使用json_decode This works with JSON you've shown: 这适用于您显示的JSON:

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

If New Purchase Orders Created key may be different in every JSON, use the array_first() helper: 如果每个JSON中的New Purchase Orders Created键可能不同,请使用array_first()帮助器:

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

If you will have many elements, iterate over them: 如果您有很多元素,请对其进行迭代:

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

Assuming the above JSON is an object array like this: 假设上面的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"
}]';
?>

Thus, the script will be like this: 因此,脚本将如下所示:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM