我有一个数组 $ 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
]
]
]
我的购物车页面是这样的:
@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行。
由于product_name和product_description是不同的数组,因此我无法创建一个foreach,其中总行数为2,但是垃圾邮件的行名称和正文是垃圾邮件。
注意:我在Laravel框架中进行编码,这就是为什么foreach语法看起来不同。
谢谢!
答案 0 :(得分:2)
使用@for()
迭代数组:
@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
答案 1 :(得分:0)
如果product_id
是product
数组的一致字段,则可以使用其长度作为迭代器计数来对其他键执行查找。 警告这假设您用于输出到模板的所有密钥与product_id
具有相同的长度,并且每个密钥中的每个索引都指向相同的产品。
您似乎没有quantity
的密钥,因此没有填充该字段
<?php 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="<?php echo $cart['product']['product_image'][$i]; ?>">
</div>
<div class="col-md-4 col-xs-12">
<h4><strong><?php echo $cart['product']['product_name'][$i]; ?></strong></h4>
<h4><small><?php echo $cart['product']['product_description'][$i]; ?></small></h4>
</div>
<div class="col-md-6 col-xs-12">
<div class="col-md-6 text-right">
<h4><strong>$ <?php echo $cart['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>
<?php endfor ?>
PS。不幸的是,我不熟悉Laravel语法,所以这是标准的PHP
修改强>
因为我没有和我一起使用这种格式我已经创建了一个转换器功能,可以将你的数组变成更易于管理的东西
<?php
function convertCartArray($cart){
// create new container array
$products = array();
// create array of all the keys in the old cart that you wish to convert
$keys = array('product_id', 'product_name', 'product_description', 'product_image', 'product_price');
// for each product
for ($i = 0; $i < count($cart['product']['product_id']); $i++){
// create a new product array
$product = array();
// for each of the keys
for ($j = 0; $j < count($keys); $j++){
// convert old cart product detail over to product detail
$product[$keys[$j]] = $cart['product']$keys[$j][$i];
}
// add product to productsArray
array_push($products, $product);
}
// return new products array
return $products;
}
$productsArray = convertCartArray($cart);
for ($i = 0; $i <= count($productsArray); $i++): ?>
<div class="row">
<div class="col-md-2 col-xs-12">
<img class="img-responsive" src="<?php echo $productsArray[$i]['product_image']; ?>">
</div>
<div class="col-md-4 col-xs-12">
<h4><strong><?php echo $productsArray[$i]['product_name']; ?></strong></h4>
<h4><small><?php echo $productsArray[$i]['product_description']; ?></small></h4>
</div>
<div class="col-md-6 col-xs-12">
<div class="col-md-6 text-right">
<h4><strong>$ <?php echo $productsArray[$i]['product_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>
<?php endfor ?>
答案 2 :(得分:0)
我会像这样重新格式化输入,但你需要的是@foreach($products as $product)
$products
,无论你当前的变量是什么。
如果您不想更改输入的格式(实际上,我推荐它)请使用Alexey Mezenin的回答
$products = [
['id' => '2',
'name' => 'HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook (15.6 inch, Turbo SIlver, 2.19 kg)',
'description' => 'SAMSUNG 55.88cm (22) Full HD LED TV (UA22F5100AR, 2 x HDMI, 2 x USB)',
'image' => '1481116344.jpeg',
'price' => '350',
],
['id' => '6',
'name' => '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)',
'description' => 'SAMSUNG 55.88cm (22) Full HD LED TV (UA22F5100AR, 2 x HDMI, 2 x USB)',
'image' => '1481116344.jpeg',
'price' => '200',
]
];
@foreach($products as $product)
<div class="row">
<div class="col-md-2 col-xs-12">
<img class="img-responsive" src="{{asset($product['image'])}}">
</div>
<div class="col-md-4 col-xs-12">
<h4><strong>{{$product['name']}}</strong></h4>
<h4><small>{{$product['description']}}</small></h4>
</div>
<div class="col-md-6 col-xs-12">
<div class="col-md-6 text-right">
<h4><strong>${{$product['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