[英]How do i traverse through multidimensional array in laravel 5.2
I am trying to get the data from multiple tables for the table structure given below 我正在尝试从多个表中获取以下表结构的数据
Content for the tables are 表的内容是
In above scenario I just want to fetch the array of unique movie_id for the city_id=1 and my code is given below 在上述情况下,我只想为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'];
where $selectedcity->city_id is "1", I am getting all the data in bunch from above code. $ selectedcity-> city_id为“ 1”的地方,我从上面的代码中获取了所有数据。 I am also able to display single movie_id , but i want to traverse from all the multidimentional array and collect the all unique movie_id. 我还可以显示单个movie_id,但是我想遍历所有多维数组并收集所有唯一的movie_id。
kindly help me as this seems to be quite difficult to do. 请帮我,因为这似乎很难做到。 in above scenario i should get the movie_id array as [1,2]. 在上述情况下,我应该将movie_id数组设置为[1,2]。
Try using this function in your model 尝试在模型中使用此功能
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.