简体   繁体   中英

Foreach with a multidimensional array - Laravel Blade templating

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
@foreach($devices as $device)
    {{ $device->name }}

    {{ $device->views}}
@endforeach

Will be enough.

You need to echo object properties:

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

If you like to use key then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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