Laravel - 无法使用刀片从多维数组中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30065653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - can't get values from multidimensional array with blade
提问by cch
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)
.
我试图为
$b
$b
to 输出获取 0 值,Fender
Fender
但它只是为$b
$b
数组中的每个项目打印 0 位置字符:@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.
它将输出string 'Fender' (length=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;
}
}
回答by Pawel Bieszczad
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.
这应该可以工作并节省您的性能,因为我们查询所有品牌而不是单独查询每个品牌。更好的方法是建立产品关系并以这种方式获得它们。
回答by Devon
@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.
您需要查看数组的组织方式。一旦你深入到第一级,'Fender' 在 offset [0]
,'(2)' 在 offset [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 给你带来了四个字符串,而不是数组。
$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
回答by ssi-anik
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