Laravel Blade:如何在视图中循环多维数组?
Laravel Blade: How to loop multidimensional array in view?
我有以下数组,我想在 laravel blade 中以 HTML table.
的形式获取它
array:2 [▼
0 => array:6 [▼
"room" => "401"
"landmark" => "Main Building"
"capacity" => 60
"faculty" => array:4 [▼
0 => "DMSM"
1 => "DMSM"
2 => ""
3 => "DMSM"
]
"department" => array:4 [▼
0 => "BSH"
1 => "BSH"
2 => ""
3 => "BSH"
]
"course" => array:4 [▼
0 => "PHY 101"
1 => "PHY 101"
2 => "Free"
3 => "PHY 101"
]
]
1 => array:6 [▼
"room" => "504"
"landmark" => "Main Building"
"capacity" => 50
"faculty" => array:4 [▼
0 => ""
1 => ""
2 => "DMSM"
3 => ""
]
"department" => array:4 [▼
0 => ""
1 => ""
2 => "BSH"
3 => ""
]
"course" => array:4 [▼
0 => "Free"
1 => "Free"
2 => "PHY 101"
3 => "Free"
]
]
]
我从多个 table 获得了上述响应,现在我需要在视图中循环,那么如何在视图中循环上述数组?
如何以下面的格式显示上面的数组值?
没什么特别的,只是作为一个普通数组工作,并在主循环中使用另一个循环作为内部维度
您的数组如下所示。
我只是将你的数组循环到控制器中,如下所示
$array_values = [
[
"room" => "401",
"landmark" => "Main Building",
"capacity" => 60,
"faculty" => [
"DMSM",
"DMSM",
"",
"DMSM",
],
"department" => [
"BSH",
"BSH",
"",
"BSH",
],
"course" => [
"PHY 101",
"PHY 101",
"Free",
"PHY 101",
]
],
[
"room" => "504",
"landmark" => "Main Building",
"capacity" => 50,
"faculty" => [
"",
"",
"DMSM",
"",
],
"department" => [
"",
"",
"BSH",
"",
],
"course" => [
"Free",
"Free",
"PHY 101",
"Free",
]
]
];
foreach ($array_values as $values) {
echo '<br>'; // just a break to show output
foreach ($values as $key => $value) {
if (is_array($value)) { // check is value is an array which
echo $key . ' are : ';
foreach ($value as $val) {
echo $val . ' / ';
}
}
echo $value . '<br>';
}
echo '<br><br>';
}
以上代码将输出如下内容
你在 blade 中要做的事情与此类似
@foreach ($array_values as $array)
@foreach($values as $key => $value)
@if (is_array($value))
@foreach ($value as $val)
{{$val}}
@endforeach
@endif
{{$value}}
@endforeach
@endforeach
解决方案
终于找到解决办法了
<table>
@foreach($slot_wise_routine as $routine)
<tr>
<td>{{ $routine['landmark'] }}</td>
<td>{{ $routine['room'] }}</td>
<td>{{ $routine['capacity'] }}</td>
@foreach($routine['course'] as $key => $course)
<td>
{{ $course }} <br> {{ $routine['faculty'][$key] }} <br> {{ $routine['department'][$key] }}
</td>
@endforeach
</tr>
@endforeach
我有以下数组,我想在 laravel blade 中以 HTML table.
的形式获取它array:2 [▼
0 => array:6 [▼
"room" => "401"
"landmark" => "Main Building"
"capacity" => 60
"faculty" => array:4 [▼
0 => "DMSM"
1 => "DMSM"
2 => ""
3 => "DMSM"
]
"department" => array:4 [▼
0 => "BSH"
1 => "BSH"
2 => ""
3 => "BSH"
]
"course" => array:4 [▼
0 => "PHY 101"
1 => "PHY 101"
2 => "Free"
3 => "PHY 101"
]
]
1 => array:6 [▼
"room" => "504"
"landmark" => "Main Building"
"capacity" => 50
"faculty" => array:4 [▼
0 => ""
1 => ""
2 => "DMSM"
3 => ""
]
"department" => array:4 [▼
0 => ""
1 => ""
2 => "BSH"
3 => ""
]
"course" => array:4 [▼
0 => "Free"
1 => "Free"
2 => "PHY 101"
3 => "Free"
]
]
]
我从多个 table 获得了上述响应,现在我需要在视图中循环,那么如何在视图中循环上述数组?
如何以下面的格式显示上面的数组值?
没什么特别的,只是作为一个普通数组工作,并在主循环中使用另一个循环作为内部维度
您的数组如下所示。
我只是将你的数组循环到控制器中,如下所示
$array_values = [
[
"room" => "401",
"landmark" => "Main Building",
"capacity" => 60,
"faculty" => [
"DMSM",
"DMSM",
"",
"DMSM",
],
"department" => [
"BSH",
"BSH",
"",
"BSH",
],
"course" => [
"PHY 101",
"PHY 101",
"Free",
"PHY 101",
]
],
[
"room" => "504",
"landmark" => "Main Building",
"capacity" => 50,
"faculty" => [
"",
"",
"DMSM",
"",
],
"department" => [
"",
"",
"BSH",
"",
],
"course" => [
"Free",
"Free",
"PHY 101",
"Free",
]
]
];
foreach ($array_values as $values) {
echo '<br>'; // just a break to show output
foreach ($values as $key => $value) {
if (is_array($value)) { // check is value is an array which
echo $key . ' are : ';
foreach ($value as $val) {
echo $val . ' / ';
}
}
echo $value . '<br>';
}
echo '<br><br>';
}
以上代码将输出如下内容
你在 blade 中要做的事情与此类似
@foreach ($array_values as $array)
@foreach($values as $key => $value)
@if (is_array($value))
@foreach ($value as $val)
{{$val}}
@endforeach
@endif
{{$value}}
@endforeach
@endforeach
解决方案 终于找到解决办法了
<table>
@foreach($slot_wise_routine as $routine)
<tr>
<td>{{ $routine['landmark'] }}</td>
<td>{{ $routine['room'] }}</td>
<td>{{ $routine['capacity'] }}</td>
@foreach($routine['course'] as $key => $course)
<td>
{{ $course }} <br> {{ $routine['faculty'][$key] }} <br> {{ $routine['department'][$key] }}
</td>
@endforeach
</tr>
@endforeach