Chapterبەش 7 · Lectureوانە 7.3
Multidimensional Arraysڕیزەکە فرەڕەهەندەکان
Arrays can hold other arrays. Learn two-dimensional arrays as tables of rows and columns: how to read values with two indexes, count rows and columns, print them with nested for and foreach loops, avoid the off-by-one bug, use rows with named keys, and show the data as an HTML table.
دەتوانن تر هەڵبگرن. فێری ببە وەک خشتەیەکی و : چۆن بە دوو بخوێنیتەوە، و بژمێریت، بە و ناوەکی چاپیان بکەیت، خۆت لە بپارێزیت، خاوەن ناودار بەکاربهێنیت و وەک HTML پیشان بدەیت.
What you will learnچی فێر دەبیت
- Explain what a multidimensional (two-dimensional) array is.
- Create a 2D array and read or change one value with
$a[row][column]. - Count rows and columns with
count(). - Print every value with nested
forloops and with nestedforeachloops. - Recognise and fix the off-by-one error in loop conditions.
- Work with rows that are associative arrays, keeping both keys and values.
- Display a 2D array as an HTML table.
- ڕوونی بکەیتەوە () چییە.
- دروست بکەیت و یەک بە
$a[row][column]بخوێنیتەوە یان بگۆڕیت. - و بە
count()بژمێریت. - هەموو بە ناوەکی و بە ناوەکی چاپ بکەیت.
- لە بناسیتەوە و چاکی بکەیت.
- لەگەڵ ئەو کە کار بکەیت و هەم و هەم بپارێزیت.
- وەک HTML پیشان بدەیت.
Arrays inside arraysڕیزەکەکان لەناو ڕیزەکەکاندا
An element of an array can hold any value, including another array. In a multidimensional array, each element in the main array can also be an array, and each element in that sub-array can be an array again. The arrays inside are called sub-arrays.
دەتوانێت هەر هەڵبگرێت، تریش. لە دا، هەر سەرەکییەکە دەتوانێت خۆی بێت و هەر ئەو لاوەکییەش دەتوانێت دیسان بێت. ناوەوە پێیان دەوترێت لاوەکی.
An array whose elements are themselves arrays, so a value is reached with two or more keys: $table[1][2]. It is usually read with .
جۆرێکە لە کە تێیدا هەر خۆی بریتییە لە ، بۆیە بە دوو یان زیاتر کلیل دەگەیتە نرخەکان: $table[1][2]. زۆرجار بە دەخوێنرێتەوە.
The most common kind is the two-dimensional array: an array of rows, where every row is itself an array of values. It looks like a table, so we talk about rows and columns.
باوترین جۆر : کە تێدایە و هەر خۆی لە . وەک خشتەیەک دەردەکەوێت، بۆیە باسی و دەکەین.
A with two levels, like a table of rows and columns: $grid[$row][$col].
جۆرێکە لە کە دوو ئاستی هەیە، وەک خشتەیەک بە ڕیز و ستوون: $grid[$row][$col].
Think of the school timetable on the classroom wall. The whole timetable is one array. Each day is a row, and each row holds the lessons of that day. To find "the third lesson on Monday" you need two numbers: first the day (row), then the lesson (column).
بیر لە خشتەی وانەکانی سەر دیواری پۆل بکەرەوە. هەموو خشتەکە یەک . هەر ڕۆژێک و هەر وانەکانی ئەو ڕۆژە هەڵدەگرێت. بۆ دۆزینەوەی «وانەی سێیەمی دووشەممە» پێویستت بە دوو ژمارەیە: سەرەتا ڕۆژەکە ()، پاشان وانەکە ().
Where the analogy stops workingسنووری ئەم نموونەیە
A real timetable has the same number of lessons every day. In PHP each row is a separate array, so rows may have different lengths, and a row can even be an associative array.
خشتەی وانەی ڕاستەقینە هەموو ڕۆژێک هەمان ژمارە وانەی هەیە. لە PHPـدا هەر جیایە، بۆیە دەکرێت درێژیی جیاوازیان هەبێت و تەنانەت دەتوانێت بێت.
Arrays inside arraysڕیزەکەکان لەناو ڕیزەکەکاندا
The general formشێوەی گشتی
The lecture and the course notes show the general form like this:
وانەکە و تێبینییەکانی کۆرسەکە شێوە گشتییەکە بەم شێوەیە پیشان دەدەن:
$myArray = array(
array(value1, value2, value3),
array(value4, value5, value6),
array(value7, value8, value9)
);Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
$myArray(or$array_namein the notes) is the name of the whole multidimensional array.$myArray(یان$array_nameلە تێبینییەکاندا) ناوی هەموو .- Lineدێڕی 2-4
Each inner
array(...)is one sub-array: one row.هەر
array(...)ـێکی ناوەوە لاوەکییە: .
Here is the same shape with real values. To reach one value you write two indexes: first the row, then the column.
ئەمە هەمان شێوەیە بە ڕاستەقینە. بۆ گەیشتن بە یەک دوو دەنووسیت: سەرەتا ، پاشان .
<?php
$myArray = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
echo $myArray[0][2] . "<br>"; // row 0, column 2
echo $myArray[2][0]; // row 2, column 03 7
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
3<br>7Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-6
Three rows, each with three values.
سێ ، هەریەکە سێ هەیە.
- Lineدێڕی 8
$myArray[0]is the first row[1, 2, 3];[2]then picks its third value, 3.$myArray[0]یەکەم[1, 2, 3]؛ پاشان[2]سێیەم هەڵدەبژێرێت، واتە 3.- Lineدێڕی 9
Row 2, column 0: 7.
2، 0: 7.
Rows, columns and two indexesڕیزەکان، ستوونەکان و دوو ئیندێکس
The lecture's main example stores four cars. Each row holds three values: the car name (column 0), the number in stock (column 1) and the number sold (column 2).
نموونە سەرەکییەکەی وانەکە چوار ئۆتۆمبێل هەڵدەگرێت. هەر سێ هەڵدەگرێت: ناوی ئۆتۆمبێلەکە ( 0)، ژمارەی بەردەست لە کۆگادا ( 1) و ژمارەی فرۆشراو ( 2).
| Row | [0] name[0] ناو | [1] in stock[1] لە کۆگادا | [2] sold[2] فرۆشراو |
|---|---|---|---|
0 | Volvo | 22 | 18 |
1 | BMW | 15 | 13 |
2 | Saab | 5 | 2 |
3 | Land Rover | 17 | 15 |
Rows, columns and two indexesڕیزەکان، ستوونەکان و دوو ئیندێکس
Printing every value without a loopچاپکردنی هەموو نرخێک بەبێ خول
"This is how we print the above array without a loop": every value is reached with $cars[row][column].
«ئەمە ئەو شێوەیەیە کە سەرەوە بەبێ چاپ دەکەین»: هەموو بە $cars[row][column] دەگاتێ.
<?php
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";Volvo: In stock: 22, sold: 18. BMW: In stock: 15, sold: 13. Saab: In stock: 5, sold: 2. Land Rover: In stock: 17, sold: 15.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Volvo: In stock: 22, sold: 18.<br>BMW: In stock: 15, sold: 13.<br>Saab: In stock: 5, sold: 2.<br>Land Rover: In stock: 17, sold: 15.<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-8
An array of four arrays. The layout with
(on its own line is the lecture's style; any layout works.چوار . دانانی
(لە دێڕێکی جیادا شێوازی وانەکەیە؛ هەر شێوازێک کار دەکات.- Lineدێڕی 10
$cars[0][0]is "Volvo",$cars[0][1]is 22,$cars[0][2]is 18.$cars[0][0]دەکاتە "Volvo"،$cars[0][1]دەکاتە 22،$cars[0][2]دەکاتە 18.- Lineدێڕی 11-13
The same line for rows 1, 2 and 3. Only the first index changes.
هەمان دێڕ بۆ 1، 2 و 3. تەنها یەکەم دەگۆڕێت.
Writing four almost identical echo lines works for four rows, but not for 400. The next sections print the same table with loops.
نووسینی چوار دێڕی echoـی نزیک لە یەک بۆ چوار کار دەکات، بەڵام بۆ 400 نا. بەشەکانی داهاتوو هەمان خشتە بە چاپ دەکەن.
Rows, columns and two indexesڕیزەکان، ستوونەکان و دوو ئیندێکس
Counting, changing and adding rowsژماردن، گۆڕین و خستنەسەری ڕیزەکان
count($cars) counts the rows (the elements of the main array). count($cars[0]) counts the values in one row. count($cars, COUNT_RECURSIVE) counts everything: the rows and all the values inside them. You change one value with two indexes, and add a whole row with $cars[] = [...].
count($cars) دەژمێرێت ( سەرەکییەکە). count($cars[0]) یەک دەژمێرێت. count($cars, COUNT_RECURSIVE) هەموو شتێک دەژمێرێت: و هەموو ناویان. یەک بە دوو دەگۆڕیت و تەواو بە $cars[] = [...] زیاد دەکەیت.
count() لەسەر ڕیزەکێکی دووڕەهەندPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$cars = [
["Volvo", 22, 18],
["BMW", 15, 13],
["Saab", 5, 2],
["Land Rover", 17, 15],
];
echo count($cars) . " rows<br>"; // 4
echo count($cars[0]) . " columns in row 0<br>"; // 3
echo count($cars, COUNT_RECURSIVE) . " in total<br>"; // 4 rows + 12 values = 16
$cars[1][1] = 20; // change one value: BMW now has 20 in stock
$cars[] = ["Kia", 9, 4]; // add a whole new row at the end
echo $cars[1][0] . ": " . $cars[1][1] . "<br>";
echo $cars[4][0] . " is in row 4";4 rows 3 columns in row 0 16 in total BMW: 20 Kia is in row 4
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
4 rows<br>3 columns in row 0<br>16 in total<br>BMW: 20<br>Kia is in row 4Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 9
4: the main array has four elements (rows).
4: سەرەکییەکە چوار ()ی هەیە.
- Lineدێڕی 10
3: row 0 has three values.
3: 0 سێ هەیە.
- Lineدێڕی 11
16 = 4 rows + 12 values.
16 دەکاتە 4 + 12 .
- Lineدێڕی 13
Row 1, column 1 changes from 15 to 20.
1، 1 لە 15ەوە دەبێتە 20.
- Lineدێڕی 14
A new row is added at index 4.
نوێ لە 4دا زیاد دەکرێت.
Printing with nested for loopsچاپکردن بە خولەکانی forـی ناوەکی
To visit every value of a two-dimensional array, put one loop inside another: a nested loop. The outer loop goes over the rows. For each row, the inner loop goes over the columns of that row.
بۆ گەیشتن بە هەموو ، بخە ناو ترەوە: . دەرەوە بە دەڕوات. بۆ هەر ، ناوەوە بە ئەو دەڕوات.
A loop written inside another loop. For each pass of the outer loop, the inner loop runs completely; this is how you go through and tables.
بریتییە لە کە لەناو خولێکی تردا نووسراوە. بۆ هەر خولانەوەیەکی خولی دەرەوە، خولی ناوەوە بە تەواوی جێبەجێ دەبێت؛ بەم شێوەیە و خشتەکان دەخوێنرێنەوە.
- Outer loop: row 0خولی دەرەوە: ڕیزی 0
$rowis 0. Print the heading "Row number 0" and open a list.$rowدەکاتە 0. سەردێڕی «Row number 0» چاپ بکە و لیستێک بکەرەوە. - Inner loop: columns 0, 1, 2خولی ناوەوە: ستوونەکانی 0، 1، 2
$colruns 0, 1, 2 and prints$cars[0][0],$cars[0][1],$cars[0][2]: Volvo, 22, 18. Then the inner loop ends.$colبە 0، 1، 2دا دەڕوات و$cars[0][0]،$cars[0][1]،$cars[0][2]چاپ دەکات: Volvo، 22، 18. پاشان ناوەوە کۆتایی دێت. - Back to the outer loopگەڕانەوە بۆ خولی دەرەوە
Close the list.
$rowbecomes 1 and the inner loop starts again from 0 for the BMW row.لیستەکە دابخە.
$rowدەبێتە 1 و ناوەوە دیسان لە 0ەوە بۆ BMW دەست پێدەکاتەوە. - Until the last rowتا دوایین ڕیز
This repeats for rows 2 and 3. In total the inner body runs 4 × 3 = 12 times.
ئەمە بۆ 2 و 3 دووبارە دەبێتەوە. بە گشتی ناوەڕۆکی ناوەوە 4 × 3 = 12 جار جێبەجێ دەبێت.
Printing with nested for loopsچاپکردن بە خولەکانی forـی ناوەکی
Lab 4, Example 9تاقیگەی 4، نموونەی 9
<?php
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
for ($row = 0; $row < 4; $row++)
{
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++)
{
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}Row number 0 • Volvo • 22 • 18 Row number 1 • BMW • 15 • 13 Row number 2 • Saab • 5 • 2 Row number 3 • Land Rover • 17 • 15
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p><b>Row number 0</b></p><ul><li>Volvo</li><li>22</li><li>18</li></ul><p><b>Row number 1</b></p><ul><li>BMW</li><li>15</li><li>13</li></ul><p><b>Row number 2</b></p><ul><li>Saab</li><li>5</li><li>2</li></ul><p><b>Row number 3</b></p><ul><li>Land Rover</li><li>17</li><li>15</li></ul>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 9
Outer loop:
$row= 0, 1, 2, 3.دەرەوە:
$row= 0، 1، 2، 3.- Lineدێڕی 11-12
Before the values of a row: a bold heading and
<ul>.پێش : سەردێڕێکی ئەستوور و
<ul>.- Lineدێڕی 13
Inner loop:
$col= 0, 1, 2.ناوەوە:
$col= 0، 1، 2.- Lineدێڕی 15
$cars[$row][$col]: the outer counter picks the row, the inner counter picks the column.$cars[$row][$col]: دەرەوە هەڵدەبژێرێت، ناوەوە .- Lineدێڕی 17
After the inner loop: close the list.
دوای ناوەوە: لیستەکە دابخە.
Printing with nested for loopsچاپکردن بە خولەکانی forـی ناوەکی
A lab program: rows and column numbersبەرنامەیەکی تاقیگە: ژمارەی ڕیزەکان و ستوونەکان
In the lab, a student wrote a smaller 2 × 3 array and used comments to explain every position. The program prints each value with its column number:
لە تاقیگەدا خوێندکارێک بچووکتری 2 × 3 نووسی و بە هەموو شوێنێکی ڕوون کردەوە. هەر لەگەڵ ژمارەی چاپ دەکات:
<?php
$a = array(
array(0, 3, 6), //Row 0 (first row)
array(5, 7, 9) //Row 1 (second row)
);
// 0 is the first column of the first row
// 3 is the second column of the first row
// 6 is the third column of the first row
// 5 is the first column of the second row
// 7 is the second column of the second row
// 9 is the third column of the second row
// Rows
echo "<ul>";
for ($row = 0; $row <= 1; $row++) {
echo "Row num: $row";
for ($col = 0; $col <= 2; $col++) {
echo "<li>Col num ($col): ".$a[$row][$col]."</li>";
}
echo "<br />";
}
echo "</ul>";Row num: 0 • Col num (0): 0 • Col num (1): 3 • Col num (2): 6 Row num: 1 • Col num (0): 5 • Col num (1): 7 • Col num (2): 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<ul>Row num: 0<li>Col num (0): 0</li><li>Col num (1): 3</li><li>Col num (2): 6</li><br />Row num: 1<li>Col num (0): 5</li><li>Col num (1): 7</li><li>Col num (2): 9</li><br /></ul>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-5
Two rows of three numbers.
دوو هەریەکە سێ ژمارە.
- Lineدێڕی 7-13
The comments name every position: 0 is the first column of the first row, 9 is the third column of the second row.
هەموو شوێنێک ناو دەبەن: 0 یەکەم یەکەم ، 9 سێیەم دووەم .
- Lineدێڕی 17-23
Nested loops with
<= 1and<= 2: correct here, because the last indexes really are 1 and 2.بە
<= 1و<= 2: لێرەدا دروستە، چونکە دوایین بەڕاستی 1 و 2ن.
The browser shows the result fine, but the HTML is not valid: the text "Row num: 0" and the <br /> are placed directly inside <ul>, where only <li> elements are allowed. Browsers forgive this; validators do not. A cleaner version gives each row its own heading and its own list, and uses count() instead of fixed numbers:
ئەنجامەکە بە باشی پیشان دەدات، بەڵام HTMLـەکە دروست نییە: دەقی «Row num: 0» و <br /> ڕاستەوخۆ خراونەتە ناو <ul>ـەوە، لە کاتێکدا تەنها <li> ڕێگەپێدراوە. ئەمە لێدەبوورن؛ ئامرازەکانی پشکنینی HTML نا. وەشانێکی پاکتر هەر سەردێڕ و لیستی تایبەتی خۆی دەداتێ و count() لە جیاتی ژمارەی جێگیر بەکاردەهێنێت:
count()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$a = array(
array(0, 3, 6), //Row 0 (first row)
array(5, 7, 9) //Row 1 (second row)
);
for ($row = 0; $row < count($a); $row++) {
echo "<p>Row num: $row</p>";
echo "<ul>";
for ($col = 0; $col < count($a[$row]); $col++) {
echo "<li>Col num ($col): " . $a[$row][$col] . "</li>";
}
echo "</ul>";
}Row num: 0 • Col num (0): 0 • Col num (1): 3 • Col num (2): 6 Row num: 1 • Col num (0): 5 • Col num (1): 7 • Col num (2): 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Row num: 0</p><ul><li>Col num (0): 0</li><li>Col num (1): 3</li><li>Col num (2): 6</li></ul><p>Row num: 1</p><ul><li>Col num (0): 5</li><li>Col num (1): 7</li><li>Col num (2): 9</li></ul>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7
Each row gets a
<p>heading outside the list.هەر سەردێڕێکی
<p>لە دەرەوەی لیستەکە وەردەگرێت.- Lineدێڕی 8-12
A separate
<ul>per row, containing only<li>items.بۆ هەر
<ul>ـێکی جیا، کە تەنها<li>ی تێدایە.
Student question: the extra empty bulletپرسیاری خوێندکار: خاڵە بەتاڵە زیادەکە
A student printed a 2 × 3 array twice, once with nested for loops and once with nested foreach loops, and asked the teacher: why does the for version print an extra empty bullet (and warnings) after each row, while the foreach version prints the six numbers correctly? Here is the student's code:
خوێندکارێک 2 × 3ی دوو جار چاپ کرد، جارێک بە ناوەکی و جارێک بە ناوەکی و پرسیاری لە مامۆستاکەی کرد: بۆچی وەشانی for دوای هەر خاڵێکی بەتاڵی زیادە (و ) چاپ دەکات، لە کاتێکدا وەشانی foreach شەش ژمارەکە بە دروستی چاپ دەکات؟ ئەمە کۆدەکەی خوێندکارەکەیە:
<?php
$a = array(
array(0, 3, 6), //Row 0 (First Row)
array(5, 7, 9) //Row 1 (Second Row)
);
$aRowCount = count($a); //Output: 2
echo "<ul>";
for ($row = 0; $row <= $aRowCount-1; $row++) {
$aColCount = count($a[$row]);
for ($col = 0; $col <= $aColCount; $col++) {
echo "<li>".$a[$row][$col]."</li>";
}
}
echo "</ul>";
foreach($a as $numRow) {
//echo $numRow; //2
foreach($numRow as $numCol) {
echo "<li>".$numCol."</li>";
}
}
?>• 0 • 3 • 6 Warning: Undefined array key 3 in C:\xampp\htdocs\arrays\index.php on line 13 • • 5 • 7 • 9 Warning: Undefined array key 3 in C:\xampp\htdocs\arrays\index.php on line 13 • • 0 • 3 • 6 • 5 • 7 • 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<ul><li>0</li><li>3</li><li>6</li>
Warning: Undefined array key 3 in C:\xampp\htdocs\arrays\index.php on line 13
<li></li><li>5</li><li>7</li><li>9</li>
Warning: Undefined array key 3 in C:\xampp\htdocs\arrays\index.php on line 13
<li></li></ul><li>0</li><li>3</li><li>6</li><li>5</li><li>7</li><li>9</li>The file path in the message shows where your file is saved, so it will be different on your computer.
فایلەکە لەناو پەیامەکەدا شوێنی پاشەکەوتکردنی فایلەکەت پیشان دەدات، بۆیە لەسەر کۆمپیوتەرەکەی تۆ جیاواز دەبێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7
count($a)is 2: two rows.count($a)دەکاتە 2: دوو .- Lineدێڕی 10
Rows:
$row <= $aRowCount-1, so 0 and 1. Correct.:
$row <= $aRowCount-1، واتە 0 و 1. دروستە.- Lineدێڕی 11
count($a[$row])is 3: three columns.count($a[$row])دەکاتە 3: سێ .- Lineدێڕی 12
Columns:
$col <= $aColCount, so 0, 1, 2 and 3. This is the bug.:
$col <= $aColCount، واتە 0، 1، 2 و 3. ئەمەیە.- Lineدێڕی 18-23
The
foreachversion never uses indexes, so it cannot go too far.وەشانی
foreachهەرگیز بەکارناهێنێت، بۆیە ناتوانێت لە سنوور تێپەڕێت.
<= visits a column that does not exist.: <= دەچێتە سەر کە بوونی نییە.CêITStudent question: the extra empty bulletپرسیاری خوێندکار: خاڵە بەتاڵە زیادەکە
Why it happens and how to fix itبۆچی ڕوودەدات و چۆن چاک دەکرێت
- Each row has 3 values, so
count($a[$row])is 3 and the valid column indexes are 0, 1 and 2. The last index is alwayscount - 1(see count()). - The inner condition
$col <= $aColCountalso lets$colbe 3.$a[0][3]does not exist, so PHP prints Warning: Undefined array key 3 and theechoprints an empty<li></li>: that is the extra empty bullet. It happens once for each row. - This kind of bug, one step too far or one step too short, is called an off-by-one error. The fix is
$col < $aColCount(or$col <= $aColCount - 1, as the student already wrote for the rows). foreachavoids this whole problem because it never counts indexes: it simply visits the elements that exist. Preferforeachwhen you only need to read every value.- A second, smaller problem: the
foreachoutput is printed after</ul>, so those<li>items are outside any list (invalid HTML). Give the second loop its own<ul>…</ul>.
- هەر 3 هەیە، بۆیە
count($a[$row])دەکاتە 3 و دروستی 0، 1 و 2ن. دوایین هەمیشەcount - 1ـە (بڕوانە count()). - ناوەوە
$col <= $aColCountڕێگە دەدات$colببێتە 3یش.$a[0][3]بوونی نییە، بۆیە PHP Warning: Undefined array key 3 چاپ دەکات وecho<li></li>ـێکی بەتاڵ چاپ دەکات: ئەمە خاڵە بەتاڵە زیادەکەیە. بۆ هەر جارێک ڕوودەدات. - ئەم جۆرە ، هەنگاوێک زیاتر یان هەنگاوێک کەمتر، پێی دەوترێت . چارەسەرەکەی
$col < $aColCountـە (یان$col <= $aColCount - 1، وەک ئەوەی خوێندکارەکە بۆ نووسیبووی). foreachئەم کێشەیە بە تەواوی لادەبات چونکە هەرگیز ناژمێرێت: تەنها بە هەبوودا دەڕوات. کاتێک تەنها دەتەوێت هەموو بخوێنیتەوە،foreachهەڵبژێرە.- کێشەیەکی دووەمی بچووکتر:
foreachدوای</ul>چاپ دەکرێت، بۆیە ئەو<li>ـانە لە دەرەوەی هەموو لیستێکن (HTMLـی نادروست).<ul>…</ul>ـی تایبەت بدە بە دووەم.
A where a runs one time too many or one time too few, often because <= was used instead of <. With arrays it usually reads one past the end.
کە تێیدا جارێک زیاتر یان جارێک کەمتر لە پێویست دەسوڕێتەوە، زۆرجار لەبەر ئەوەی لە جیاتی < نیشانەی <= بەکارهاتووە. لەگەڵ زۆرجار دوای کۆتایی دەخوێنێتەوە.
<?php
$a = array(
array(0, 3, 6), //Row 0 (First Row)
array(5, 7, 9) //Row 1 (Second Row)
);
$aRowCount = count($a);
echo "<ul>";
for ($row = 0; $row < $aRowCount; $row++) {
$aColCount = count($a[$row]);
for ($col = 0; $col < $aColCount; $col++) { // < not <=
echo "<li>" . $a[$row][$col] . "</li>";
}
}
echo "</ul>";
echo "<ul>"; // a second, separate list
foreach ($a as $numRow) {
foreach ($numRow as $numCol) {
echo "<li>" . $numCol . "</li>";
}
}
echo "</ul>";• 0 • 3 • 6 • 5 • 7 • 9 • 0 • 3 • 6 • 5 • 7 • 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<ul><li>0</li><li>3</li><li>6</li><li>5</li><li>7</li><li>9</li></ul><ul><li>0</li><li>3</li><li>6</li><li>5</li><li>7</li><li>9</li></ul>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 11
<instead of<=: columns 0, 1, 2 only.<لە جیاتی<=: تەنها 0، 1، 2.- Lineدێڕی 17
The
foreachversion now has its own list.وەشانی
foreachئێستا لیستی تایبەتی خۆی هەیە.
Nested foreach loopsخولەکانی foreachـی ناوەکی
With foreach you do not need indexes at all. The outer foreach gives you one whole row (a sub-array) on each turn; the inner foreach goes through the values of that row. This is the general pattern from the course notes:
بە foreach هیچ پێویستت بە نییە. foreachی دەرەوە لە هەر تەواو (یەک لاوەکی) دەداتێ؛ foreachی ناوەوە بە ئەو دەڕوات. ئەمە شێوە گشتییەکەیە لە تێبینییەکانی کۆرسەکەوە:
foreach ($array_name as $sub_array) {
foreach ($sub_array as $element) {
echo $element;
}
}<?php
$parentArray = array(
array(1, 2, 3),
array(4, 5, 6)
);
foreach ($parentArray as $subArray) {
foreach ($subArray as $itemInSubArray) {
echo $itemInSubArray;
}
}123456
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
Turn 1:
$subArrayis[1, 2, 3]. Turn 2:[4, 5, 6].جاری 1:
$subArrayدەکاتە[1, 2, 3]. جاری 2:[4, 5, 6].- Lineدێڕی 9-10
The inner loop prints each number of the current row; with no separator they run together.
ناوەوە هەر ژمارەیەکی ئێستا چاپ دەکات؛ بێ جیاکەرەوە پێکەوە دەلکێن.
Nested foreach loopsخولەکانی foreachـی ناوەکی
The lecture's example: carsنموونەکەی وانەکە: ئۆتۆمبێلەکان
The lecture uses the same idea on a cars array. Here the columns are a name, a number and a year; the lecture does not say what the middle number means.
وانەکە هەمان بیرۆکە لەسەر ئۆتۆمبێل بەکاردەهێنێت. لێرەدا ناوێک، ژمارەیەک و ساڵێکن؛ وانەکە ناڵێت ژمارە ناوەڕاستەکە چ واتایەکی هەیە.
foreachـی ناوەکیی وانەکەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$cars = array(
array("volvo", 1, 1988),
array("BMW", 1, 1990),
array("Nissan", 3, 2000),
array("Jaguar", 4, 2010)
);
foreach ($cars as $key => $values) {
foreach ($values as $value) {
echo " $value";
}
echo "<br>";
}volvo 1 1988 BMW 1 1990 Nissan 3 2000 Jaguar 4 2010
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
volvo 1 1988<br> BMW 1 1990<br> Nissan 3 2000<br> Jaguar 4 2010<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
$keyreceives 0, 1, 2, 3 but is never used;foreach ($cars as $values)would be enough.$key0، 1، 2، 3 وەردەگرێت بەڵام هەرگیز بەکارناهێنرێت؛foreach ($cars as $values)بەس دەبوو.- Lineدێڕی 9-11
Each value is printed after five spaces.
هەر دوای پێنج چاپ دەکرێت.
- Lineدێڕی 12
<br>after each row: one row per line.<br>دوای هەر : هەر لە دێڕێکدا.
Nested foreach loopsخولەکانی foreachـی ناوەکی
Student question: echo of a whole rowپرسیاری خوێندکار: echoی ڕیزێکی تەواو
Rows with named keysڕیزەکان بە کلیلەکانی ناودار
The rows of a multidimensional array can be associative arrays. Then every row describes one thing with named fields, like one user with a name and an age. Inside the inner foreach you can use $key => $value to print both:
دەتوانن بن. ئەو کاتە هەر یەک شت بە بەشی ناودار وەسف دەکات، وەک یەک بەکارهێنەر بە ناو و تەمەنەوە. لەناو foreachی ناوەوەدا دەتوانیت $key => $value بەکاربهێنیت بۆ چاپکردنی هەردووکیان:
foreachی ناوەکی: دەرەوە وەردەگرێت، ناوەوە یەک .CêIT<?php
$users = array(
array("name" => "Aram", "age" => 30),
array("name" => "Shilan", "age" => 25),
array("name" => "Karwan", "age" => 40)
);
foreach ($users as $user) {
foreach ($user as $key => $value) {
echo $key . ": " . $value . "<br>";
}
echo "<br>";
}name: Aram age: 30 name: Shilan age: 25 name: Karwan age: 40
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
name: Aram<br>age: 30<br><br>name: Shilan<br>age: 25<br><br>name: Karwan<br>age: 40<br><br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
Outer loop:
$useris one whole user (an associative array).دەرەوە:
$userیەک بەکارهێنەری تەواوە ().- Lineدێڕی 9-10
Inner loop:
$keyis "name" or "age",$valueis its value.ناوەوە:
$keyدەکاتە "name" یان "age"،$value.- Lineدێڕی 12
An extra
<br>leaves an empty line between users.<br>ـێکی زیادە دێڕێکی بەتاڵ لەنێوان بەکارهێنەراندا دادەنێت.
Rows with named keysڕیزەکان بە کلیلەکانی ناودار
The row index and named valuesئیندێکسی ڕیز و نرخەکانی ناودار
You often know which keys each row has. Then one loop is enough: read the values directly with $user["name"] and $user["age"]. The outer foreach can also give you the index of each row with $index => $user:
زۆرجار دەزانیت هەر چ هەیە. ئەو کاتە یەک بەسە: ڕاستەوخۆ بە $user["name"] و $user["age"] بخوێنەوە. foreachی دەرەوە دەتوانێت ی هەر پێ بدات بە $index => $user:
<?php
$users = array(
array("name" => "Aram", "age" => 30),
array("name" => "Shilan", "age" => 25),
array("name" => "Karwan", "age" => 40)
);
foreach ($users as $index => $user) {
echo "User " . ($index + 1) . ": " . $user["name"] . " is " . $user["age"] . " years old.<br>";
}User 1: Aram is 30 years old. User 2: Shilan is 25 years old. User 3: Karwan is 40 years old.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
User 1: Aram is 30 years old.<br>User 2: Shilan is 25 years old.<br>User 3: Karwan is 40 years old.<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
$indexis 0, 1, 2 and$useris the row at that index.$indexدەکاتە 0، 1، 2 و$userئەو کە لەو .- Lineدێڕی 9
$index + 1turns 0, 1, 2 into the friendlier 1, 2, 3. The brackets make PHP add before joining strings.$index + 10، 1، 2 دەکاتە 1، 2، 3ی ئاسانتر بۆ خوێنەر. کەوانەکان وا دەکەن PHP پێش لکاندنی کۆ بکاتەوە.
The same idea works with a for loop and indexes. In this student example each row is [first name, last name, age]:
هەمان بیرۆکە بە و کار دەکات. لەم نموونەی خوێندکارێکدا هەر [ناوی یەکەم، ناوی کۆتایی، تەمەن]ـە:
for چاپ کراون (ناوەکان گۆڕدراون)PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$students = array(
array("Aram", "Karim", 22),
array("Shilan", "Aziz", 21),
array("Karwan", "Omar", 23),
array("Dilan", "Saeed", 20),
array("Rezan", "Ali", 22)
);
for ($i = 0; $i < count($students); $i++) {
echo "Student " . ($i + 1) . ": ";
echo $students[$i][0] . " " . $students[$i][1] . ", " . $students[$i][2] . " years old<br>";
}Student 1: Aram Karim, 22 years old Student 2: Shilan Aziz, 21 years old Student 3: Karwan Omar, 23 years old Student 4: Dilan Saeed, 20 years old Student 5: Rezan Ali, 22 years old
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Student 1: Aram Karim, 22 years old<br>Student 2: Shilan Aziz, 21 years old<br>Student 3: Karwan Omar, 23 years old<br>Student 4: Dilan Saeed, 20 years old<br>Student 5: Rezan Ali, 22 years old<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 9
count($students)is 5. It is called on every turn; storing it in a variable first (as Lab 4 does) is a little faster.count($students)دەکاتە 5. لە هەموو بانگ دەکرێت؛ هەڵگرتنی لە پێشتر (وەک تاقیگەی 4) کەمێک خێراترە.- Lineدێڕی 10
$i + 1shows the student number starting from 1.$i + 1ژمارەی خوێندکار لە 1ەوە پیشان دەدات.- Lineدێڕی 11
$students[$i][0]first name,[1]last name,[2]age.$students[$i][0]ناوی یەکەم،[1]ناوی کۆتایی،[2]تەمەن.
Rows with named keysڕیزەکان بە کلیلەکانی ناودار
Student question: why "5060"?پرسیاری خوێندکار: بۆچی «5060»؟
Rows with named keysڕیزەکان بە کلیلەکانی ناودار
A total for each rowکۆکە بۆ هەر ڕیزێک
Nested loops also let you calculate something for each row, for example a student's total and best mark:
هەروەها ڕێگەت پێدەدەن بۆ هەر شتێک حیساب بکەیت، بۆ نموونە کۆی نمرەکان و باشترین نمرەی خوێندکارێک:
<?php
$marks = [
"Aram" => [90, 85, 77],
"Shilan" => [68, 92, 81],
];
foreach ($marks as $name => $subjects) {
$total = 0;
foreach ($subjects as $mark) {
$total += $mark;
}
echo "$name: total $total, highest " . max($subjects) . "<br>";
}Aram: total 252, highest 90 Shilan: total 241, highest 92
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Aram: total 252, highest 90<br>Shilan: total 241, highest 92<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
Reset the total at the start of every row.
لە سەرەتای هەر کۆکە سفر بکەرەوە.
- Lineدێڕی 12
max($subjects)works on one row, because one row is an ordinary array.max($subjects)لەسەر یەک کار دەکات، چونکە یەک ئاساییە.
Showing a 2D array as an HTML tableپیشاندانی ڕیزەکێکی دووڕەهەند وەک خشتەیەکی HTML
A two-dimensional array already is a table, so the natural way to show it in a page is an HTML <table>: one <tr> for each row of the array and one <td> for each value. A student's notes show this with both loop types (names and data changed here):
خۆی خشتەیەکە، بۆیە ڕێگا سروشتییەکە بۆ پیشاندانی لە لاپەڕەدا <table>ـی HTMLـە: یەک <tr> بۆ هەر و یەک <td> بۆ هەر . تێبینییەکانی خوێندکارێک ئەمە بە هەردوو جۆری پیشان دەدەن (ناوەکان و زانیارییەکان لێرەدا گۆڕدراون):
foreachPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$cars = array(
array("Ford", "2017", "Aram"),
array("Kia", "2015", "Shilan"),
array("Toyota", "1996", "Karwan")
);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Car</th>";
echo "<th>Year</th>";
echo "<th>Owner</th>";
echo "</tr>";
foreach ($cars as $car) {
echo "<tr>";
echo "<td>" . $car[0] . "</td>";
echo "<td>" . $car[1] . "</td>";
echo "<td>" . $car[2] . "</td>";
echo "</tr>";
}
echo "</table>";Car | Year | Owner Ford | 2017 | Aram Kia | 2015 | Shilan Toyota | 1996 | Karwan
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<table border='1'><tr><th>Car</th><th>Year</th><th>Owner</th></tr><tr><td>Ford</td><td>2017</td><td>Aram</td></tr><tr><td>Kia</td><td>2015</td><td>Shilan</td></tr><tr><td>Toyota</td><td>1996</td><td>Karwan</td></tr></table>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
Open the table.
border='1'draws simple borders.بکەرەوە.
border='1'چوارچێوەی سادە دەکێشێت.- Lineدێڕی 10-14
A header row with three
<th>cells.سەردێڕ بە سێ
<th>.- Lineدێڕی 16-22
One
<tr>per car;$car[0],$car[1],$car[2]fill the three cells.بۆ هەر ئۆتۆمبێلێک
<tr>ـێک؛$car[0]،$car[1]،$car[2]سێ پڕ دەکەنەوە.- Lineدێڕی 24
Close the table after the loop.
دوای دابخە.
for لەگەڵ count()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$cars = array(
array("Ford", "2017", "Aram"),
array("Kia", "2015", "Shilan"),
array("Toyota", "1996", "Karwan")
);
echo "<table border='1'>";
echo "<tr><th>Car</th><th>Year</th><th>Owner</th></tr>";
for ($i = 0; $i < count($cars); $i++) {
echo "<tr>";
echo "<td>" . $cars[$i][0] . "</td>";
echo "<td>" . $cars[$i][1] . "</td>";
echo "<td>" . $cars[$i][2] . "</td>";
echo "</tr>";
}
echo "</table>";Car | Year | Owner Ford | 2017 | Aram Kia | 2015 | Shilan Toyota | 1996 | Karwan
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<table border='1'><tr><th>Car</th><th>Year</th><th>Owner</th></tr><tr><td>Ford</td><td>2017</td><td>Aram</td></tr><tr><td>Kia</td><td>2015</td><td>Shilan</td></tr><tr><td>Toyota</td><td>1996</td><td>Karwan</td></tr></table>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
The student left this version as a comment; it produces exactly the same rows.
خوێندکارەکە ئەم وەشانەی وەک هێشتبووەوە؛ ڕێک هەمان دروست دەکات.
- Lineدێڕی 12-14
$cars[$i][0]… uses the counter as the row index.$cars[$i][0]… وەک بەکاردەهێنێت.
Showing a 2D array as an HTML tableپیشاندانی ڕیزەکێکی دووڕەهەند وەک خشتەیەکی HTML
Rows of any lengthڕیزەکان بە هەر درێژییەک
If the rows can have any length, use a nested foreach for the cells too:
ئەگەر دەکرێت هەر درێژییەکیان هەبێت، بۆ foreachی ناوەکی بەکاربهێنە:
foreachی ناوەکیPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$marks = [
["Aram", 90, 85, 77],
["Shilan", 68, 92, 81],
["Karwan", 74, 70, 88],
];
echo "<table border='1'>";
echo "<tr><th>Name</th><th>PHP</th><th>Networks</th><th>Databases</th></tr>";
foreach ($marks as $row) {
echo "<tr>";
foreach ($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>";
}
echo "</table>";Name | PHP | Networks | Databases Aram | 90 | 85 | 77 Shilan | 68 | 92 | 81 Karwan | 74 | 70 | 88
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<table border='1'><tr><th>Name</th><th>PHP</th><th>Networks</th><th>Databases</th></tr><tr><td>Aram</td><td>90</td><td>85</td><td>77</td></tr><tr><td>Shilan</td><td>68</td><td>92</td><td>81</td></tr><tr><td>Karwan</td><td>74</td><td>70</td><td>88</td></tr></table>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
Outer loop: one
<tr>per row.دەرەوە: بۆ هەر
<tr>ـێک.- Lineدێڕی 12-14
Inner loop: one
<td>per value.ناوەوە: بۆ هەر
<td>ـێک.
Common mistakesهەڵە باوەکان
<=withcount()in aforloop: one index too far, Undefined array key. Use<, orforeach.- Swapping the indexes:
$cars[2][1](row 2, column 1) is not$cars[1][2]. Row first, then column. echoof a row (echo $cars[1];): printsArraywith a warning. Use a second index, an inner loop orprint_r().- Hard-coded sizes (
$row < 4): break when rows are added. Usecount($cars)andcount($cars[$row]). - Forgetting to reset a total or counter at the start of each row: the totals add up across rows.
- No separator: two echoes such as
50and60look like one number,5060. - Losing the outer key:
foreach ($mark as $m)drops the names; useforeach ($mark as $name => $m). - Invalid list HTML: text,
<br>or<li>items outside<ul>…</ul>. Only<li>belongs directly inside<ul>. - Typing template placeholders such as
value1as real code: Undefined constant. - Expecting spaces to line up in the browser: several spaces collapse into one. Use an HTML table.
<=لەگەڵcount()لە : زیاتر لە پێویست، Undefined array key.<یانforeachبەکاربهێنە.- گۆڕینەوەی شوێنی :
$cars[2][1]( 2، 1) وەک$cars[1][2]نییە. سەرەتا ، پاشان . echoی (echo $cars[1];):Arrayلەگەڵ چاپ دەکات. دووەم، ناوەوە یانprint_r()بەکاربهێنە.- قەبارەی جێگیر (
$row < 4): کاتێک زیاد دەکرێت دەشکێت.count($cars)وcount($cars[$row])بەکاربهێنە. - لەبیرچوونی سفرکردنەوەی کۆکە یان لە سەرەتای هەر : کۆکەکان لە پێشووەوە کۆ دەبنەوە.
- بێ جیاکەرەوە: دوو
echoی وەک50و60وەک یەک ژمارە دەردەکەون،5060. - ونکردنی دەرەوە:
foreach ($mark as $m)ناوەکان فڕێ دەدات؛foreach ($mark as $name => $m)بەکاربهێنە. - HTMLـی نادروستی لیست: دەق،
<br>یان<li>لە دەرەوەی<ul>…</ul>. تەنها<li>ڕاستەوخۆ دەچێتە ناو<ul>. - نووسینی جێگرەکان وەک
value1وەک کۆدی ڕاستەقینە: Undefined constant. - چاوەڕوانیی ڕێکبوونی لە : چەند دەبنە یەک. HTML بەکاربهێنە.
Exercisesڕاهێنانەکان
Lab 4, Example 9. Store the four cars (Volvo 22 18, BMW 15 13, Saab 5 2, Land Rover 17 15) in a two-dimensional array. Print them with nested for loops: for each row a bold heading "Row number N", followed by a bullet list of the three values of that row.
تاقیگەی 4، نموونەی 9. چوار ئۆتۆمبێلەکە (Volvo 22 18، BMW 15 13، Saab 5 2، Land Rover 17 15) لە هەڵبگرە. بە ناوەکی چاپیان بکە: بۆ هەر سەردێڕێکی ئەستووری «Row number N» و لەدوایدا بە سێ ئەو .
Hintئاماژە
Outer loop over the rows, inner loop over the columns; $cars[$row][$col] reads one value.
دەرەوە بە ، ناوەوە بە ؛ $cars[$row][$col] یەک دەخوێنێتەوە.
Show answerوەڵامەکە پیشان بدە
count())PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$cars = [
["Volvo", 22, 18],
["BMW", 15, 13],
["Saab", 5, 2],
["Land Rover", 17, 15],
];
$rows = count($cars);
for ($row = 0; $row < $rows; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
$cols = count($cars[$row]);
for ($col = 0; $col < $cols; $col++) {
echo "<li>" . $cars[$row][$col] . "</li>";
}
echo "</ul>";
}Row number 0 • Volvo • 22 • 18 Row number 1 • BMW • 15 • 13 Row number 2 • Saab • 5 • 2 Row number 3 • Land Rover • 17 • 15
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p><b>Row number 0</b></p><ul><li>Volvo</li><li>22</li><li>18</li></ul><p><b>Row number 1</b></p><ul><li>BMW</li><li>15</li><li>13</li></ul><p><b>Row number 2</b></p><ul><li>Saab</li><li>5</li><li>2</li></ul><p><b>Row number 3</b></p><ul><li>Land Rover</li><li>17</li><li>15</li></ul>Expected result: four bold headings, "Row number 0" to "Row number 3", each followed by a three-item list. The lab's own version with < 4 and < 3 gives exactly the same output.
ئەنجامی چاوەڕوانکراو: چوار سەردێڕی ئەستوور، «Row number 0» تا «Row number 3»، هەریەکە و لیستێکی سێ دانەیی لەدوایدا. وەشانەکەی خودی تاقیگەکە بە < 4 و < 3 ڕێک هەمان دەدات.
Store a 2 × 3 array (0, 3, 6 / 5, 7, 9) and print every value with its row number and column number, as in the lab program. Make sure the HTML is valid.
2 × 3 (0، 3، 6 / 5، 7، 9) هەڵبگرە و هەر لەگەڵ ژمارەی و ژمارەی چاپ بکە، وەک تاقیگەکە. دڵنیابە کە HTMLـەکە دروستە.
Show answerوەڵامەکە پیشان بدە
<?php
$a = array(
array(0, 3, 6), //Row 0 (first row)
array(5, 7, 9) //Row 1 (second row)
);
for ($row = 0; $row < count($a); $row++) {
echo "<p>Row num: $row</p>";
echo "<ul>";
for ($col = 0; $col < count($a[$row]); $col++) {
echo "<li>Col num ($col): " . $a[$row][$col] . "</li>";
}
echo "</ul>";
}Row num: 0 • Col num (0): 0 • Col num (1): 3 • Col num (2): 6 Row num: 1 • Col num (0): 5 • Col num (1): 7 • Col num (2): 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Row num: 0</p><ul><li>Col num (0): 0</li><li>Col num (1): 3</li><li>Col num (2): 6</li></ul><p>Row num: 1</p><ul><li>Col num (0): 5</li><li>Col num (1): 7</li><li>Col num (2): 9</li></ul>Fix the bug: this loop prints an empty bullet and a warning. Why, and what is the fix?
چاک بکە: ئەم خاڵێکی بەتاڵ و چاپ دەکات. بۆچی و چارەسەرەکەی چییە؟
<?php
$row = [4, 8, 15];
for ($i = 0; $i <= count($row); $i++) {
echo "<li>" . $row[$i] . "</li>";
}Show answerوەڵامەکە پیشان بدە
count($row) is 3, so the valid indexes are 0, 1 and 2. <= also visits 3, which does not exist. Change <= to <, or simply use foreach ($row as $value).
count($row) دەکاتە 3، بۆیە دروست 0، 1 و 2ن. <= دەچێتە سەر 3یش، کە بوونی نییە. <= بگۆڕە بۆ <، یان بە سادەیی foreach ($row as $value) بەکاربهێنە.
Create an associative array $prices of three shops, where each shop is an associative array of item => price (for example "Tea" => 1000). Print each shop name, then its items and prices, using nested foreach with =>.
بە ناوی $prices بۆ سێ دوکان دروست بکە، کە هەر دوکانێک لە کاڵا => نرخ (بۆ نموونە "Tea" => 1000). ناوی هەر دوکانێک چاپ بکە، پاشان کاڵاکان و نرخەکانیان، بە foreachی ناوەکی لەگەڵ =>.
Show answerوەڵامەکە پیشان بدە
<?php
$prices = [
"Karwan's shop" => ["Tea" => 1000, "Sugar" => 2500],
"Hana's shop" => ["Rice" => 3000, "Bread" => 500],
"Rezan's shop" => ["Tea" => 1250, "Oil" => 4000],
];
foreach ($prices as $shop => $items) {
echo "<b>$shop</b><br>";
foreach ($items as $item => $price) {
echo "$item: $price dinars<br>";
}
}Karwan's shop Tea: 1000 dinars Sugar: 2500 dinars Hana's shop Rice: 3000 dinars Bread: 500 dinars Rezan's shop Tea: 1250 dinars Oil: 4000 dinars
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<b>Karwan's shop</b><br>Tea: 1000 dinars<br>Sugar: 2500 dinars<br><b>Hana's shop</b><br>Rice: 3000 dinars<br>Bread: 500 dinars<br><b>Rezan's shop</b><br>Tea: 1250 dinars<br>Oil: 4000 dinars<br>Summary and reviewکورتە و پێداچوونەوە
- A multidimensional array is an array whose elements are arrays. A two-dimensional array is a table of rows and columns.
- Read one value with two indexes:
$a[row][column].$a[row]alone is a whole row (an array). count($a)counts rows;count($a[$row])counts the values of one row;COUNT_RECURSIVEcounts everything.- Use nested loops: the outer loop over rows, the inner loop over the values of the current row.
- With
for, the condition is< count(...);<=causes an off-by-one error.foreachcannot go out of range. - Rows can be associative arrays: use
foreach ($rows as $index => $row)and$row["name"], or an innerforeach ($row as $key => $value). - An HTML table is the natural way to display a 2D array: one
<tr>per row, one<td>per value.
- کە . خشتەیەکی و .
- یەک بە دوو بخوێنەوە:
$a[row][column].$a[row]بە تەنها تەواوە (). count($a)دەژمێرێت؛count($a[$row])یەک دەژمێرێت؛COUNT_RECURSIVEهەموو شتێک دەژمێرێت.- بەکاربهێنە: دەرەوە بە ، ناوەوە بە ئێستادا.
- لەگەڵ
for،< count(...)ـە؛<=دروست دەکات.foreachناتوانێت لە سنوور دەربچێت. - دەتوانن بن:
foreach ($rows as $index => $row)و$row["name"]بەکاربهێنە، یانforeach ($row as $key => $value)ی ناوەوە. - HTML ڕێگای سروشتییە بۆ پیشاندانی : بۆ هەر
<tr>ـێک، بۆ هەر<td>ـێک.
What does $cars[3][0] return in the lecture's cars array?لە ڕیزەکی ئۆتۆمبێلەکانی وانەکەدا $cars[3][0] چی دەگەڕێنێتەوە؟
"Land Rover": row 3 is the fourth car, and column 0 is the name.
"Land Rover": 3 چوارەم ئۆتۆمبێلە و 0 ناوەکەیە.
Why can a nested for loop print "Undefined array key" warnings when foreach does not?بۆچی خولی forـی ناوەکی دەتوانێت هۆشدارییەکانی «Undefined array key» چاپ بکات بەڵام foreach نا؟
for uses numbers that you compute; a wrong condition (<=) asks for an index that does not exist. foreach only visits elements that exist.
for ژمارە بەکاردەهێنێت کە خۆت حیسابیان دەکەیت؛ نادروست (<=) داوای دەکات کە بوونی نییە. foreach تەنها بە هەبوودا دەڕوات.
How many times does the inner loop body run for a 4 × 3 array?بۆ ڕیزەکێکی 4 × 3، ناوەڕۆکی خولی ناوەوە چەند جار جێبەجێ دەبێت؟
4 × 3 = 12 times: three times for each of the four rows.
4 × 3 = 12 جار: سێ جار بۆ هەریەک لە چوار .
How do you keep the outer key (for example a student name) in a nested foreach?چۆن کلیلی دەرەوە (بۆ نموونە ناوی خوێندکار) لە foreachی ناوەکیدا دەپارێزیت؟
Write foreach ($mark as $name => $m); then $name holds the key and $m the inner array.
بنووسە foreach ($mark as $name => $m)؛ ئەو کاتە $name هەڵدەگرێت و $m ناوەوە.
Why does " $value" not show five spaces in the browser?بۆچی " $value" لە تۆڕگەڕدا پێنج بۆشایی پیشان نادات؟
HTML collapses several spaces into one. Use a table (or CSS) for layout.
HTML چەند لەدوای یەک دەکاتە یەک. بۆ شێوەدان (یان CSS) بەکاربهێنە.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
How to put arrays inside an array, read a value with two indexes, and build rows that use named keys.
چۆن بخەیتە ناو ، بە دوو بخوێنیتەوە و دێڕەکان بە ناودار دروست بکەیت.
Watch 0:00–8:30: arrays of arrays, then rows with named keys
لە 0:00 تا 8:30 سەیر بکە: لەناو ، پاشان دێڕەکان بە ناودار.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Sorting Arraysڕیزکردنی ڕیزەکەکان
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.