我具有以下多维数组,但无法执行foreach循环(使用laravel)。我想显示名字。 任何想法如何循环通过该数组以仅显示名称?我缩小了显示的数组-> ...
我想让该数组不在视图中而是在控制器中循环,因为我想为每个客户端创建一个数据库条目
array:1 [▼
"client" => array:52 [▼
0 => array:11 [▼
"name" => "Company One"
...
]
1 => array:11 [▼
"name" => "Company 2"
...
]
感谢您的帮助。
答案 0 :(得分:1)
$array = [
'client' => [
[
'name' => 'Company One',
'foo' => 'Foo One',
],[
'name' => 'Company 2',
'foo' => 'Foo 2',
]
]
];
$names = array_pluck($array['client'], 'name');
foreach($names as $name) {
echo $name; // Replace this with the logic to create DB entry
}
答案 1 :(得分:0)
这很容易,您只需假设您的数组在变量$ myArray中
$myArray = [▼
"client" => array:52 [▼
0 => array:11 [▼
"name" => "Company One"
...
]
1 => array:11 [▼
"name" => "Company 2"
...
]
然后您必须做:
@foreach ($myArray->client as $data)
{{$data->name}}
@endforeach