如何在Laravel中循环遍历此数组?

时间:2016-11-05 13:49:09

标签: php arrays laravel laravel-5.2 laravel-5.3

我正在使用laravel 5.3处理Web应用程序,我遇到了一个问题。

我想遍历这个数组。

我尝试的是:

控制器:

  public function postSearch(Request $request)
    {

        $categoryid = $request->category_id;
        $locationid = $request->location_id;
        $category = Category::where('id', $categoryid)->first();
        $cities = Location::where('id', $locationid)->pluck('city');
        $cityname = $cities[0];

        $alllocations = [];
        $ratecards = [];
        $locations = [];

        $father = [];

        $i = 0;
        $filteredlocations = $category->locations->where('city', $cityname)->all();
        foreach($filteredlocations as $location){

            $alllocations[$i] = $location->getaddetails;
            $ratecards[$i] = $location->ratecard;
            $i++;
        }

        $father[0] = $alllocations;
        $father[1] = $ratecards;

        return view('ads', compact('father'));

    }

查看在此处:

@foreach($father as  $key => $f)

                     @foreach($f as $key => $arr)

                        @if (isset($arr->adName))
                            <div class="post">

                                {{Html::image($arr->path,'Ad Picture',array('class' => 'ad-image'))}}
                                <a href="{{ url('ads', $arr->location_id)  }}"><h2 class="post-title">{{ $arr->adName }}</h2></a>

                                <p class="description">{{ $arr->description }} </p>

                                <div class="post-footer">
                                    <span class="categories">
                                        <ul class="cats">
                                           <li class="btn btn-default">Price : 
                                            {{ $f[$key]->monthly_rate }}
                                           </li>
                                           <li class="btn btn-default">Status: 
                                               @if($arr->status == 1) 
                                                    <p>Active</p>
                                               @else
                                                    <p>Not-Active</p>
                                               @endif
                                           </li>
                                            <li><a href="{{ url('ads', $arr->location_id)  }}" class="details-page">view details</a></li>

                                        </ul>
                                    </span>
                                </div>

                            </div>
                        @endif
                         @endforeach

                    @endforeach

问题:

好的问题是当我试图得到{{ $f[$key]->monthly_rate }}这是第二个数组时,即 RateCards 我什么都没得到。我想获取价目表数组,并从该数组mothly_rate获取该字段。

由于

PS: {{ $f[$key]->monthly_rate }}这会让人失望。

1 个答案:

答案 0 :(得分:0)

问题是$ f [$ key]是两个对象的数组。 这意味着您要查找的属性位于每个项目中,如下所示:

tred

您必须循环这些对象才能获取其属性值。