Foreach with a multidimensional array - Laravel Blade templating

梦想与她 提交于 2019-12-25 08:33:13

问题


I have the following array result set, I'm trying to loop through each of the results and just echo them out onto the page. I'm using Laravel 5.2 and the blade templating engine

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"
    ]
  ]
}

This is what I have so far

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

回答1:


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

    {{ $device->views}}
@endforeach

Will be enough.




回答2:


You need to echo object properties:

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



回答3:


If you like to use key then

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


来源:https://stackoverflow.com/questions/41976902/foreach-with-a-multidimensional-array-laravel-blade-templating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!