如何在laravel 5.2中遍历多维数组

时间:2016-09-24 13:41:26

标签: php arrays multidimensional-array laravel-5 laravel-5.2

我正在尝试从多个表中获取下面给出的表结构的数据

enter image description here

表的内容是

enter image description here

在上面的场景中,我只想获取city_id = 1的唯一movie_id数组,我的代码如下所示

 $movies=General_cities::with('cinemahall.showtime')->where('city_id',$selectedcity->city_id)->get();

 return $movies[0]['cinemahall'][1]['showtime'][0]['movie_id'];

其中$ selectedcity-> city_id为“1”,我从上面的代码中获取了所有数据。我也可以显示单个movie_id,但是我想遍历所有的multidimentional数组并收集所有唯一的movie_id。

请帮助我,因为这似乎很难做到。 在上面的场景中,我应该将movie_id数组作为[1,2]。

1 个答案:

答案 0 :(得分:0)

尝试在模型中使用此功能

public function unique_movie_id()
{

return $this->join('movies_cinemahall', 'movies_cinemahall.city_id','=','general_cities.id')->join('movies_showtime', 'movies_showtime.cinema_id','=','movies_cinemahall.cinema_id')->join('movies','movies.movie_id','=','movies_showtime.movie_id')->distinct('movies.movie_id')->select('movies.id')->get();

}