本文介绍了如何将数组从控制器传递到视图(Laravel 5)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组($ response_array)是这样的:

My array ($response_array) is like this :

Array
(
    [Auth] => Array
        (
            [UserID] => test
            [UserIP] => 123
            [XmlVersion] => Testing Version
        )

    [SearchAvailRequest] => Array
        (
            [CountryCd] => ID
            [CityCd] => JOG
            [CheckIn] => 2016-01-08
            [CheckOut] => 2016-01-09
        )

    [SearchAvailResponse] => Array
        (
            [Hotel] => Array
                (
                    [0] => Array
                        (
                            [HotelNo] => 1
                            [HotelCode] => 321
                            [HotelName] => Test 1 Hotel
                            [RmGrade] => Deluxe
                            [RmGradeCode] => DLX
                        )

                    [1] => Array
                        (
                            [HotelNo] => 2
                            [HotelCode] => 212
                            [HotelName] => Test 2 & 1 Hotel
                            [RmGrade] => Deluxe
                            [RmGradeCode] => DLX
                        )

                )

        )

)

我想发送酒店数据以查看

I want to send the hotel data to view

因此视图显示的数据如下:

So the view display data like this :

Hotel Name
Room Type

我这样尝试:

控制器:

...
return view('frontend.hotel.main_list_hotel',[
                                'hotel' => $response_array
            ]);
...

查看:

@foreach($hotel as $key=>$value)
{{ $key }}
{{ $value }}                     
@endforeach

但是,存在这样的错误:

But, there exist error like this :

htmlentities() expects parameter 1 to be string, array given (View: C:\xampp\htdocs...

关于如何解决此问题的任何建议?

Any suggestions on how I can solve this problem?

非常感谢您

推荐答案

如果要遍历酒店,则需要遍历 $ response_array ['SearchAvailResponse'] ['Hotel'] ,而不是只是 $ response_array .

If you want to iterate through hotels you need to iterate through $response_array['SearchAvailResponse']['Hotel'], not just $response_array.

尝试通过以下方式传递酒店参数:

Try passing hotel parameter as the following:

return view('frontend.hotel.main_list_hotel',[
  'hotel' => $response_array['SearchAvailResponse']['Hotel']
]);

然后在您看来:

@foreach($hotel as $key=>$value)
{{ $key }}
{{ $value['HotelNo'] }}                     
@endforeach

这篇关于如何将数组从控制器传递到视图(Laravel 5)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:13