繁体   English   中英

具有多维数组的Foreach-Laravel Blade模板

[英]Foreach with a multidimensional array - Laravel Blade templating

我有以下数组结果集,我试图遍历每个结果,并将它们回显到页面上。 我正在使用Laravel 5.2和刀片模板引擎

Collection {#240 ▼
  #items: array:3 [▼
    0 => array:2 [▼
      "name" => "desktop"
      "views" => "349"
    ]
    1 => array:2 [▼
      "name" => "mobile"
      "views" => "151"
    ]
    2 => array:2 [▼
      "name" => "tablet"
      "views" => "68"
    ]
  ]
}

这就是我到目前为止

@foreach($devices as $device)
    $key = 0; $key++; $key < 2;
    {{ $device[$key] }},
@endforeach
@foreach($devices as $device)
    {{ $device->name }}

    {{ $device->views}}
@endforeach

足够了。

您需要回显对象属性:

@foreach($devices as $device)
    {{ $device->name }} has {{ $device->views }}
@endforeach

如果你想用钥匙

@foreach($devices as $key => $val)
     {{ $device[$key]->name }},
     {{ $device[$key]->views }}
@endforeach

暂无
暂无

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

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