在laravel中调用2维数组中的元素

时间:2017-12-31 15:09:04

标签: laravel

items:array:2 [▼

0 => Tour {#229 ▼
  #table: "tour"
  #...
  #attributes: array:12 [▼
    "id" => 6
    "name" => "Nha Trang Tour"
    "price" => 3500000.0
    "price_promotion" => 0.0
    "id_package" => 4
    "id_location" => 8
    "start_time" => "2018-01-27"
    "end_time" => "2018-01-28"
    "star" => 4.0
    "description" => "Funny"
    "created_at" => null
    "updated_at" => "2017-12-29 00:00:00"
  ]
  #...
}
1 => Tour {#230 ▶}

我需要在"#items"中使用2个数组0和1中的2个id_location,我使用

($sale['items']->id_location);

($sale->id_location);

但不能接受,我使用

($sale[0]->id_location);

然后成功,但只得到一个

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

使用pluck()方法(如果它是一个集合)或array_pluck如果它是一个数组:

$sale->pluck('id_location');
array_pluck($sales, 'id_location');

答案 1 :(得分:0)

非常感谢你:D,我成功地拿走了桌子, 但是当我使用视图时,案例是:在[" Weekend Package"中查看]而不是Weekend Package enter image description here