【PHP】24. 巢狀迴圈
【PHP】24. 巢狀迴圈
此文章也有影片介紹,可以搭配影片一起學習!
01. 巢狀迴圈 (所屬課程)
甚麼是巢狀迴圈?
巢狀迴圈就是一個迴圈裡面還有另一個迴圈,它通常拿來印表格,向下面這樣單純的表格,表格整體是第一層陣列(紅色大括弧),小明、老王、小美的姓名與成績分別是第二陣列(藍色大括弧),
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
?>
那我們現在用foreach先把姓名給印出來,
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
foreach($students as $student){
echo $student["姓名"];
}
?>
輸出結果:
現在把姓名給印出來了,接下來要把成績都給印出來,不過因為第二層的陣列裡有key和value,那我們該怎麼做呢?其實foreach也有辦法可以把key和value同時印出來,我們可以這樣寫
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
foreach($students as $student){
foreach($student as $key => $value){
echo"{$key}:{$value}";
}
}
?>
輸出結果:
確實都印出來了,不過排版有點奇怪,我們幫它加個表格與線條吧!
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
echo "<table border=1>";
echo "<tbody>";
foreach($students as $student){
echo "<tr>";
foreach($student as $key => $value){
echo "<td>";
echo"{$key}:{$value}";
echo "</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
輸出結果:
表格印出來了!
不過這樣子寫巢狀迴圈的話在你真的在維護的時候會蠻痛苦的,那我們要如何降低痛苦呢?我們可以先把它改成function的形式
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
function createRow($student){
echo "<tr>";
foreach($student as $key => $value){
echo "<td>";
echo"{$key}:{$value}";
echo "</td>";
}
echo "</tr>";
}
function createTable($students){
echo "<table border=1>";
echo "<tbody>";
foreach($students as $student){
createRow($student);
}
echo "</tbody>";
echo "</table>";
}
createTable($students);
?>
思路:
1.宣告一個函數createRow(),將建立row的程式碼放進去,並呼叫這個函數,將傳入值$student放入。
2.同樣的宣告一個函數createTable(),將建立table的程式碼放進去,並呼叫這個函數,將傳入值$students放入。
輸出結果:
答案是一模一樣的。
這樣看起來程式碼變多了,為什麼會說這樣會比較好維護呢?答案是因為這個table表和裡面的值沒有任何的關聯,等於我建立了一個印表格的程式,也就是說如果我想要在建立另外一個table表(例如$products),就算裡面有更多的欄位或值,我都可以把它印出來,我只要把裡面的變數改掉就好。
現在我再用同樣的程式碼來建立名為$products的表格:
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
$products = [
[
"品名" => "手機",
"顏色" => "藍色",
"數量" => 1,
"價錢" => 3000,
],
[
"品名" => "電腦",
"顏色" => "白色",
"數量" => 2,
"價錢" => 30000,
],
[
"品名" => "電視",
"顏色" => "紅色",
"數量" => 8,
"價錢" => 20000,
],
[
"品名" => "印表機",
"顏色" => "黑色",
"數量" => 10,
"價錢" => 1000,
],
];
function createRow($row){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>";
echo"{$key}:{$value}";
echo "</td>";
}
echo "</tr>";
}
function createTable($data){
echo "<table border=1>";
echo "<tbody>";
foreach($data as $row){
createRow($row);
}
echo "</tbody>";
echo "</table>";
}
createTable($products);
?>
輸出結果:
我也可以用這個方法同時把兩個表格印出來:
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
$products = [
[
"品名" => "手機",
"顏色" => "藍色",
"數量" => 1,
"價錢" => 3000,
],
[
"品名" => "電腦",
"顏色" => "白色",
"數量" => 2,
"價錢" => 30000,
],
[
"品名" => "電視",
"顏色" => "紅色",
"數量" => 8,
"價錢" => 20000,
],
[
"品名" => "印表機",
"顏色" => "黑色",
"數量" => 10,
"價錢" => 1000,
],
];
function createRow($row){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>";
echo"{$key}:{$value}";
echo "</td>";
}
echo "</tr>";
}
function createTable($data){
echo "<table border=1>";
echo "<tbody>";
foreach($data as $row){
createRow($row);
}
echo "</tbody>";
echo "</table>";
}
createTable($products);
createTable($students);
?>
輸出結果:
兩個表格都被印出來了!
這樣的寫法比較有結構性,而且如果我只想改變row或改變table的時候,我只要找到相關function裡面的程式碼就行了。那如果我想把表格的標題給印出來該怎麼做呢?
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
$products = [
[
"品名" => "手機",
"顏色" => "藍色",
"數量" => 1,
"價錢" => 3000,
],
[
"品名" => "電腦",
"顏色" => "白色",
"數量" => 2,
"價錢" => 30000,
],
[
"品名" => "電視",
"顏色" => "紅色",
"數量" => 8,
"價錢" => 20000,
],
[
"品名" => "印表機",
"顏色" => "黑色",
"數量" => 10,
"價錢" => 1000,
],
];
function createRow($row){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>";
echo"{$key}:{$value}";
echo "</td>";
}
echo "</tr>";
}
function createTable($data){
if(count($data)<1){
return;
}
echo "<table border=1>";
echo "<thead>";
echo "<tr>";
foreach( $data[0] as $key => $value){
echo "<th>";
echo $key;
echo "</th>";
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach($data as $row){
createRow($row);
}
echo "</tbody>";
echo "</table>";
}
createTable($products);
createTable($students);
?>
思路:
1.印出<thead>和<tr>
2.利用if條件判斷若$data小於1則甚麼都不做
3.利用foreach迴圈將<th>和裡面的$key給印出來
輸出結果:
標題被正常印出來了。
而因為陣列至少要有一個,所以如果怕看不懂的話,我們也可以把$data[0]賦予一個變數$firstRow,並且建立一個函數createTableHead ():
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
$products = [
[
"品名" => "手機",
"顏色" => "藍色",
"數量" => 1,
"價錢" => 3000,
],
[
"品名" => "電腦",
"顏色" => "白色",
"數量" => 2,
"價錢" => 30000,
],
[
"品名" => "電視",
"顏色" => "紅色",
"數量" => 8,
"價錢" => 20000,
],
[
"品名" => "印表機",
"顏色" => "黑色",
"數量" => 10,
"價錢" => 1000,
],
];
function createRow($row){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>";
echo"{$key}:{$value}";
echo "</td>";
}
echo "</tr>";
}
function createTableHead($firstRow){
echo "<thead>";
echo "<tr>";
foreach($firstRow as $key => $value){
echo "<th>";
echo $key;
echo "</th>";
}
echo "</tr>";
echo "</thead>";
}
function createTable($data){
if(count($data)<1){
return;
}
echo "<table border=1>";
$firstRow =$data[0];
createTableHead($firstRow);
echo "<tbody>";
foreach($data as $row){
createRow($row);
}
echo "</tbody>";
echo "</table>";
}
createTable($products);
createTable($students);
?>
輸出結果:
我們也可以把印出的echo"{$key}:{$value}"中的{$key}拿掉,印出來就像一般常見的表格:
$students = [
[
"姓名" => "小明",
"數學成績" => 30,
"英文成績" => 20,
"歷史成績" => 91,
],
[
"姓名" => "老王",
"數學成績" => 89,
"英文成績" => 82,
"歷史成績" => 83,
],
[
"姓名" => "小美",
"數學成績" => 65,
"英文成績" => 99,
"歷史成績" => 87,
],
];
$products = [
[
"品名" => "手機",
"顏色" => "藍色",
"數量" => 1,
"價錢" => 3000,
],
[
"品名" => "電腦",
"顏色" => "白色",
"數量" => 2,
"價錢" => 30000,
],
[
"品名" => "電視",
"顏色" => "紅色",
"數量" => 8,
"價錢" => 20000,
],
[
"品名" => "印表機",
"顏色" => "黑色",
"數量" => 10,
"價錢" => 1000,
],
];
function createRow($row){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>";
echo "{$value}";
echo "</td>";
}
echo "</tr>";
}
function createTableHead($firstRow){
echo "<thead>";
echo "<tr>";
foreach( $firstRow as $key => $value){
echo "<th>";
echo $key;
echo "</th>";
}
echo "</tr>";
echo "</thead>";
}
function createTable($data){
if (count($data) < 1){
return;
}
echo "<table border=1>";
createTableHead($data[0]);
echo "<tbody>";
foreach($data as $row){
createRow($row);
}
echo "</tbody>";
echo "</table>";
}
createTable($products);
createTable($students);
?>
輸出結果:
所以如果想把thead印出來就必須要多做一點事情,但程式的邏輯還沒有很通的話,可以先把tbody給列出來就好,並多多練習利用函數讓程式碼更乾淨易懂。
最後,如果你喜歡我們的文章,別忘了到我們的FB粉絲團按讚喔!!