输出多维数组

时间:2016-03-17 15:48:52

标签: php laravel

我正在尝试调用此数组,但我得到一个数组到字符串转换错误。我不确定我做错了什么,我只是在学习。如果有人能帮我正确设置,我将非常感激!

阵列:

public function getPricing()
{
    $windows = array("Storm Windows" => array("(includes removal, cleaning, 
                                            and replacement)", " - $12.00")
                    , "Double Hung" => array("(opens up and down)" ,
                                             " - $9.00")
                    , "Casement" => array("(crank out or stationary
                                             window)" ," - $8.00")
                    , "Transom" => array("(rectangle window over top of a   
                                             window)" ," - $3.00")
                    );


  return view('pages.pricing')
      ->with("windows", $windows);

}

在视图中调用它:

   @foreach ($windows as $window => $DescriptionPrice)
        <h2>{{$window . $DescriptionPrice}}</h2>
   @endforeach

2 个答案:

答案 0 :(得分:0)

$DescriptionPrice是一个数组,因此无法输出为字符串。你需要做这样的事情: -

@foreach ($windows as $window => $DescriptionPrice)
    <h2>{{$window}} {{$DescriptionPrice[0]}} {{$DescriptionPrice[1]}}</h2>
@endforeach 

答案 1 :(得分:0)

试试这段代码:你想要输出一个不是字符串的数组。因此,您需要选择需要打印的数组项,如下所示。

@foreach ($windows as $window => $DescriptionPrice)
   <h2>For {{ $window }} - {{$DescriptionPrice[0]}} - {{$DescriptionPrice[1]}}</h2>
@endforeach