如何在foreach中使用多维数组? [英] How do I use the multidimensional array in foreach?
本文介绍了如何在foreach中使用多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组 $ cart :
array:1 [
"product" => array:5 [
"product_id" => array:2 [
0 => 2
1 => 6
]
"product_name" => array:2 [
0 => "HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook (15.6 inch, Turbo SIlver, 2.19 kg)"
1 => "SAMSUNG 55.88cm (22) Full HD LED TV (UA22F5100AR, 2 x HDMI, 2 x USB)"
]
"product_description" => array:2 [
0 => "HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook (15.6 inch, Turbo SIlver, 2.19 kg)HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook (15.6 inch, Turbo SIlver, 2.19 kg)"
1 => "SAMSUNG 55.88cm (22) Full HD LED TV (UA22F5100AR, 2 x HDMI, 2 x USB)"
]
"product_image" => array:2 [
0 => "1481116344.jpeg"
1 => "1481180186.jpeg"
]
"product_price" => array:2 [
0 => 350
1 => 200
]
]
]
我的购物车页面是这样的:
My cart page is something like this:
@foreach()
<div class="row">
<div class="col-md-2 col-xs-12">
<img class="img-responsive" src="{{asset('images/150x70.png')}}">
</div>
<div class="col-md-4 col-xs-12">
<h4><strong>Product Name</strong></h4>
<h4><small>Product Body</small></h4>
</div>
<div class="col-md-6 col-xs-12">
<div class="col-md-6 text-right">
<h4><strong>$ Price</strong> x</h4>
</div>
<div class="col-md-4 col-xs-9">
<input type="text" class="form-control input-sm" placeholder="quantity">
</div>
<div class="col-md-2 col-xs-2">
<button class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></button>
</div>
</div>
</div>
<hr>
@endforeach
输出看起来像
如何在此代码块中对多维数组值进行垃圾邮件处理.假设有3个乘积,则应该有3行.
How do I spam the multidimentional array values in this block of code. Suppose there are 3 products then there should be 3 rows.
由于product_name和product_description是不同的数组,因此我无法在总行数保持为2的情况下创建foreach,但它会为行的正确名称和正文发送垃圾邮件.
注意:我正在Laravel框架中进行编码,这就是为什么foreach语法似乎不同的原因.
Note: I am coding in Laravel framework that is why foreach syntax seems different.
谢谢!
推荐答案
使用 @for()
遍历数组:
Use @for()
to iterate over array:
@for ($i = 0; $i < count($cart['product']['product_id']); $i++)
<div class="row">
<div class="col-md-2 col-xs-12">
<img class="img-responsive" src="{{ asset('images/150x70.png') }}">
</div>
<div class="col-md-4 col-xs-12">
<h4><strong>{{ $card['product']['product_name'][$i] }}</strong></h4>
<h4><small>{{ $card['product']['product_description'][$i] }}</small></h4>
</div>
<div class="col-md-6 col-xs-12">
<div class="col-md-6 text-right">
<h4><strong>$ {{ $card['product']['product_price'][$i] }}</strong> x</h4>
</div>
<div class="col-md-4 col-xs-9">
<input type="text" class="form-control input-sm" placeholder="quantity">
</div>
<div class="col-md-2 col-xs-2">
<button class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></button>
</div>
</div>
</div>
<hr>
@endfor
这篇关于如何在foreach中使用多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文