I am having trouble looping through a multidimensional array with blade in laravel. I am sending the data from the controller like so:
在laravel上,我在用刀片进行多维数组循环时遇到了麻烦。我从控制器发送数据如下:
return View::make('store.categories')
->with('brands', $brands);
And if I die dump the data:
如果我把数据丢了:
array (size=2)
0 =>
array (size=2)
0 => string 'Fender' (length=6)
1 => string '(2)' (length=3)
1 =>
array (size=2)
0 => string 'Gibson' (length=6)
1 => string '(1)' (length=3)
I've tried to use two @foreach
loops but I couldn't get it to work:
我试过使用两个@foreach循环,但是我不能让它工作:
@foreach($brands as $brand)
@foreach($brand as $b)
{{$b}}
@endforeach
@endforeach
The above will output: Fender (2) Gibson (1)
.
以上将输出:Fender (2) Gibson(1)。
I tried to get the 0 value for the
$b
to output
Fender
but it just prints the 0 position character for each of the items in the
$b
array:
@foreach($brands as $brand)
@foreach($brand as $b)
{{$b[0]}}
@endforeach
@endforeach
The above will output F ( G (
.
上面将输出F (G ()
In my controller if I do:
在我的控制器中,如果我:
foreach ($brands as $b) {
foreach($b as $key=>$v) {
dd($v);
}
}
it will output string 'Fender' (length=6)
, which seems like the second loop inside the first @foreach
works. Although, when it comes to the blade code mentioned above it doesn't.
它将输出字符串“Fender”(长度=6),这似乎是第一个@foreach工作的第二个循环。但是,当提到上面提到的刀片代码时,它没有。
I'm probably doing something terribly wrong. How can I get the output for the values 0 and 1 for the nested arrays individually? Any help is highly appreciated.
我可能做错了什么。如何分别获得嵌套数组的值0和1的输出?非常感谢您的帮助。
This is how I create the data in my controller's function:
这就是我如何在我的控制器函数中创建数据:
$products = Product::with('brand')->whereIn('category_id', $children->lists('id'));
$brand_ids = array();
$brands = array();
foreach ($products->get() as $p) {
$brand_ids[] = $p->brand_id;
}
$brand_count = array_count_values($brand_ids);
foreach ($brand_count as $key=>$value) {
$query = Brand::where('id', '=', $key)->lists('name');
// dd($query);
foreach($query as $key=>$name) {
$array = array(
$name,
'('.$value.')'
);
$brands[] = $array;
}
}
3 个解决方案
#1
5
Controller
控制器
$brands = Brand::whereIn('id', $brand_ids)->lists('name', 'id');
Blade
叶片
@foreach($brands as $id => $brand)
Id: {{$id}}, Brand: {{$brand}}
@endforeach
This should work and save you performance, cause we query all the brands instead of each one individually. A better approach would be to have the products relation set up and get them that way.
这应该可以工作并保存您的性能,因为我们查询所有的品牌而不是每个单独的品牌。一种更好的方法是让产品关系建立起来并以这种方式得到它们。
#2
0
@foreach($brands as $brand)
{{$brand[0]}}
@endforeach
You need to look at how the array is organized. Once you dig into the first level, 'Fender' is at offset [0]
and '(2)' is at offset [1]
, so you only need one foreach.
您需要了解数组是如何组织的。一旦你深入到第一层,“挡泥板”在偏移[0],“(2)”在偏移[1],所以你只需要一个foreach。
The reason you were getting F
is because you were getting the offset [0]
on the string 'Fender' (or in other words, the first letter) because the second foreach was bringing you four strings, not arrays.
你得到F的原因是因为你得到了字符串'Fender'(或者换句话说,第一个字母)上的偏移[0],因为第二个foreach给你带来了4个字符串,而不是数组。
$brands = [
0 =>
[
0 => 'Fender',
1 => '(2)'
],
1 =>
[
0 => 'Gibson',
1 => '(1)'
]
];
var_dump($brands);
foreach($brands as $brand) {
echo $brand[0]."\n";
}
Outputs:
输出:
array(2) {
[0]=>
array(2) {
[0]=>
string(6) "Fender"
[1]=>
string(3) "(2)"
}
[1]=>
array(2) {
[0]=>
string(6) "Gibson"
[1]=>
string(3) "(1)"
}
}
Fender
Gibson
#3
0
as you want to get the 2nd level values of multi dimensional array, and if you know that 2nd level will have only two values, then why don't you try
当你想要得到二维数组的第二层值时,如果你知道第二层只有两个值,那你为什么不试试呢
@foreach($brands as $brand)
{{$brand[0]}} {{$brand[1]}}
@endforeach
#1
5
Controller
控制器
$brands = Brand::whereIn('id', $brand_ids)->lists('name', 'id');
Blade
叶片
@foreach($brands as $id => $brand)
Id: {{$id}}, Brand: {{$brand}}
@endforeach
This should work and save you performance, cause we query all the brands instead of each one individually. A better approach would be to have the products relation set up and get them that way.
这应该可以工作并保存您的性能,因为我们查询所有的品牌而不是每个单独的品牌。一种更好的方法是让产品关系建立起来并以这种方式得到它们。
#2
0
@foreach($brands as $brand)
{{$brand[0]}}
@endforeach
You need to look at how the array is organized. Once you dig into the first level, 'Fender' is at offset [0]
and '(2)' is at offset [1]
, so you only need one foreach.
您需要了解数组是如何组织的。一旦你深入到第一层,“挡泥板”在偏移[0],“(2)”在偏移[1],所以你只需要一个foreach。
The reason you were getting F
is because you were getting the offset [0]
on the string 'Fender' (or in other words, the first letter) because the second foreach was bringing you four strings, not arrays.
你得到F的原因是因为你得到了字符串'Fender'(或者换句话说,第一个字母)上的偏移[0],因为第二个foreach给你带来了4个字符串,而不是数组。
$brands = [
0 =>
[
0 => 'Fender',
1 => '(2)'
],
1 =>
[
0 => 'Gibson',
1 => '(1)'
]
];
var_dump($brands);
foreach($brands as $brand) {
echo $brand[0]."\n";
}
Outputs:
输出:
array(2) {
[0]=>
array(2) {
[0]=>
string(6) "Fender"
[1]=>
string(3) "(2)"
}
[1]=>
array(2) {
[0]=>
string(6) "Gibson"
[1]=>
string(3) "(1)"
}
}
Fender
Gibson
#3
0
as you want to get the 2nd level values of multi dimensional array, and if you know that 2nd level will have only two values, then why don't you try
当你想要得到二维数组的第二层值时,如果你知道第二层只有两个值,那你为什么不试试呢
@foreach($brands as $brand)
{{$brand[0]}} {{$brand[1]}}
@endforeach