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 پیشان بدەیت.

23 min read21 خولەک خوێندنەوە · 23 stepsهەنگاو

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 for loops and with nested foreach loops.
  • 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.

دەتوانێت هەر هەڵبگرێت، تریش. لە دا، هەر سەرەکییەکە دەتوانێت خۆی بێت و هەر ئەو لاوەکییەش دەتوانێت دیسان بێت. ناوەوە پێیان دەوترێت لاوەکی.

Definitionپێناسە

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.

باوترین جۆر : کە تێدایە و هەر خۆی لە . وەک خشتەیەک دەردەکەوێت، بۆیە باسی و دەکەین.

Definitionپێناسە

A with two levels, like a table of rows and columns: $grid[$row][$col].

جۆرێکە لە کە دوو ئاستی هەیە، وەک خشتەیەک بە ڕیز و ستوون: $grid[$row][$col].

The school timetableخشتەی وانەکانی قوتابخانە

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:

وانەکە و تێبینییەکانی کۆرسەکە شێوە گشتییەکە بەم شێوەیە پیشان دەدەن:

General form (a template, not real code)شێوەی گشتی (قاڵبێک، نەک کۆدی ڕاستەقینە)TEXT
$myArray = array(
    array(value1, value2, value3),
    array(value4, value5, value6),
    array(value7, value8, value9)
);
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

$myArray (or $array_name in 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.

ئەمە هەمان شێوەیە بە ڕاستەقینە. بۆ گەیشتن بە یەک دوو دەنووسیت: سەرەتا ، پاشان .

A 3 × 3 arrayڕیزەکێکی ⁦3 × 3⁩PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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 0
Explain 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⁩.

Quick checkپشکنینی خێرا$m = [[1, 2], [3, 4], [5, 6]]; What does echo $m[2][0]; print?$m = [[1, 2], [3, 4], [5, 6]]; ئەم کۆدە echo $m[2][0]; چی چاپ دەکات؟

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⁩).

A grid for $cars with 4 rows and 3 columns. Column 0 is the name, column 1 the number in stock, column 2 the number sold. Rows: Volvo 22 18; BMW 15 13; Saab 5 2; Land Rover 17 15. Row 1 (BMW) is outlined and labelled $cars[1], the whole row. The cell with 13 is labelled $cars[1][2]: row 1, then column 2. Below: $cars[row][column], first the row number, then the column number.خشتەیەک بۆ $cars بە 4 ڕیز و 3 ستوون. ستوونی 0 ناوە، ستوونی 1 ژمارەی لە کۆگادا، ستوونی 2 ژمارەی فرۆشراو. ڕیزەکان: Volvo 22 18؛ BMW 15 13؛ Saab 5 2؛ Land Rover 17 15. ڕیزی 1 (BMW) دەوری گیراوە و ناوی $cars[1]ـە، واتە هەموو ڕیزەکە. ئەو خانەیەی 13ی تێدایە ناوی $cars[1][2]ـە: ڕیزی 1، پاشان ستوونی 2. لە خوارەوە: $cars[row][column]، سەرەتا ژمارەی ڕیز، پاشان ژمارەی ستوون.
A two-dimensional array as a grid: the first index chooses the row, the second the column. وەک خشتەیەک: یەکەم هەڵدەبژێرێت، دووەمیان .CêIT
The data in $cars ناو $cars
Row[0] name⁦[0]⁩ ناو[1] in stock⁦[1]⁩ لە کۆگادا[2] sold⁦[2]⁩ فرۆشراو
0Volvo2218
1BMW1513
2Saab52
3Land Rover1715

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] دەگاتێ.

Printing every value by handچاپکردنی هەموو نرخێک بە دەستPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
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() on a 2D arraycount() لەسەر ڕیزەکێکی دووڕەهەند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";
Explain 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دا زیاد دەکرێت.

Quick checkپشکنینی خێرا$m = [[1, 2], [3, 4], [5, 6]]; What is count($m)?$m = [[1, 2], [3, 4], [5, 6]]; ئەنجامی count($m) چەندە؟

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.

بۆ گەیشتن بە هەموو ، بخە ناو ترەوە: . دەرەوە بە دەڕوات. بۆ هەر ، ناوەوە بە ئەو دەڕوات.

Definitionپێناسە

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.

بریتییە لە کە لەناو خولێکی تردا نووسراوە. بۆ هەر خولانەوەیەکی خولی دەرەوە، خولی ناوەوە بە تەواوی جێبەجێ دەبێت؛ بەم شێوەیە و خشتەکان دەخوێنرێنەوە.

How the nested loops runخولە ناوەکییەکان چۆن کار دەکەن
  1. Outer loop: row 0خولی دەرەوە: ڕیزی 0

    $row is 0. Print the heading "Row number 0" and open a list.

    $row دەکاتە 0. سەردێڕی «⁦Row number 0⁩» چاپ بکە و لیستێک بکەرەوە.

  2. Inner loop: columns 0, 1, 2خولی ناوەوە: ستوونەکانی 0، 1، 2

    $col runs 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⁩. پاشان ناوەوە کۆتایی دێت.

  3. Back to the outer loopگەڕانەوە بۆ خولی دەرەوە

    Close the list. $row becomes 1 and the inner loop starts again from 0 for the BMW row.

    لیستەکە دابخە. $row دەبێتە 1 و ناوەوە دیسان لە 0ەوە بۆ BMW دەست پێدەکاتەوە.

  4. 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

Lab 4, Example 9: two-dimensional arrayتاقیگەی 4، نموونەی 9: ڕیزەکی دووڕەهەندPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
}
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⁩ نووسی و بە هەموو شوێنێکی ڕوون کردەوە. هەر لەگەڵ ژمارەی چاپ دەکات:

The lab program (as written)بەرنامەی تاقیگە (وەک نووسراوە)PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
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 <= 1 and <= 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() لە جیاتی ژمارەی جێگیر بەکاردەهێنێت:

Valid HTML and count()HTMLـی دروست و 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>";
}
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 شەش ژمارەکە بە دروستی چاپ دەکات؟ ئەمە کۆدەکەی خوێندکارەکەیە:

The student's codeکۆدی خوێندکارەکەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
        }
    }
?>
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 foreach version never uses indexes, so it cannot go too far.

وەشانی foreach هەرگیز بەکارناهێنێت، بۆیە ناتوانێت لە سنوور تێپەڕێت.

The array $a with rows [0] and [1] and columns [0], [1], [2] holding 0, 3, 6 and 5, 7, 9. A fourth column [3] is drawn dashed with question marks and the label "does not exist". count($a[0]) gives 3; valid indexes are 0, 1, 2. The loop $col <= $aColCount visits 0, 1, 2 and 3; visiting 3 gives "Warning: Undefined array key 3" and an empty list item. The loop $col < $aColCount visits 0, 1, 2 and is correct.ڕیزەکەکە $a پیشان دراوە بە ڕیزەکانی [0] و [1] و ستوونەکانی [0]، [1]، [2] کە 0، 3، 6 و 5، 7، 9 هەڵدەگرن. ستوونێکی چوارەم [3] بە هێڵی پچڕپچڕ و نیشانەی پرسیار کێشراوە و نووسراوە «بوونی نییە». count($a[0]) دەداتە 3؛ ئیندێکسەکانی دروست 0، 1، 2ن. خولەکە $col <= $aColCount بە 0، 1، 2 و 3دا دەڕوات؛ گەیشتن بە 3 «Warning: Undefined array key 3» و خاڵێکی بەتاڵی لیست دەدات. خولەکە $col < $aColCount بە 0، 1، 2دا دەڕوات و دروستە.
An off-by-one bug: <= visits a column that does not exist.: <= دەچێتە سەر کە بوونی نییە.CêIT

Student 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 always count - 1 (see count()).
  • The inner condition $col <= $aColCount also lets $col be 3. $a[0][3] does not exist, so PHP prints Warning: Undefined array key 3 and the echo prints 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).
  • foreach avoids this whole problem because it never counts indexes: it simply visits the elements that exist. Prefer foreach when you only need to read every value.
  • A second, smaller problem: the foreach output 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>ـی تایبەت بدە بە دووەم.
Definitionپێناسە

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.

کە تێیدا جارێک زیاتر یان جارێک کەمتر لە پێویست دەسوڕێتەوە، زۆرجار لەبەر ئەوەی لە جیاتی < نیشانەی <= بەکارهاتووە. لەگەڵ زۆرجار دوای کۆتایی دەخوێنێتەوە.

The corrected programبەرنامەکە ڕاستکراوەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11

< instead of <=: columns 0, 1, 2 only.

< لە جیاتی <=: تەنها 0، 1، 2.

Lineدێڕی 17

The foreach version now has its own list.

وەشانی foreach ئێستا لیستی تایبەتی خۆی هەیە.

Quick checkپشکنینی خێراWhich loop condition visits every column of $a[$row] exactly once?کام ڕێک جارێک بە هەموو $a[$row]دا دەڕوات؟

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ی ناوەوە بە ئەو دەڕوات. ئەمە شێوە گشتییەکەیە لە تێبینییەکانی کۆرسەکەوە:

General formشێوەی گشتیTEXT
foreach ($array_name as $sub_array) {
    foreach ($sub_array as $element) {
        echo $element;
    }
}
Example from the notesنموونە لە تێبینییەکانەوەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$parentArray = array(
    array(1, 2, 3),
    array(4, 5, 6)
);

foreach ($parentArray as $subArray) {
    foreach ($subArray as $itemInSubArray) {
        echo $itemInSubArray;
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8

Turn 1: $subArray is [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.

ناوەوە هەر ژمارەیەکی ئێستا چاپ دەکات؛ بێ جیاکەرەوە پێکەوە دەلکێن.

Quick checkپشکنینی خێراIn foreach ($a as $row) { foreach ($row as $v) { ... } }, what is $row on each turn of the outer loop?لە foreach ($a as $row) { foreach ($row as $v) { ... } }دا، $row لە هەر دەرەوەدا چییە؟

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.

وانەکە هەمان بیرۆکە لەسەر ئۆتۆمبێل بەکاردەهێنێت. لێرەدا ناوێک، ژمارەیەک و ساڵێکن؛ وانەکە ناڵێت ژمارە ناوەڕاستەکە چ واتایەکی هەیە.

The lecture's nested foreachforeachـی ناوەکیی وانەکە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>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8

$key receives 0, 1, 2, 3 but is never used; foreach ($cars as $values) would be enough.

$key 0، 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ی ڕیزێکی تەواو

Quick checkپشکنینی خێرا$m = [[1, 2], [3, 4], [5, 6]]; What does foreach ($m as $r) { echo $r; } print?$m = [[1, 2], [3, 4], [5, 6]]; ئەم کۆدە foreach ($m as $r) { echo $r; } چی چاپ دەکات؟

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 بەکاربهێنیت بۆ چاپکردنی هەردووکیان:

The array $users holds three inner arrays at indexes [0], [1] and [2]. Each inner array has the key "name" with "Aram", "Shilan" or "Karwan" and the key "age" with 30, 25 or 40. Arrows mark outer turns 1, 2 and 3 of foreach ($users as $user): each turn gives one whole row. Numbers 1 and 2 on the keys "name" and "age" of the first row show the inner loop foreach ($user as $key => $value) visiting the pairs of one row in order.ڕیزەکەکە $users سێ ڕیزەکی ناوەوە لە ئیندێکسەکانی [0]، [1] و [2]دا هەڵدەگرێت. هەر ڕیزەکێکی ناوەوە کلیلی "name"ی هەیە لەگەڵ "Aram"، "Shilan" یان "Karwan" و کلیلی "age" لەگەڵ 30، 25 یان 40. تیرەکان جاری 1، 2 و 3ی خولی دەرەوە foreach ($users as $user) نیشان دەدەن: هەر جارێک یەک ڕیزی تەواو دەدات. ژمارەکانی 1 و 2 لەسەر کلیلەکانی "name" و "age"ی یەکەم ڕیز پیشان دەدەن کە خولی ناوەوە foreach ($user as $key => $value) یەک لە دوای یەک بە جووتەکانی یەک ڕیزدا دەڕوات.
Nested foreach: the outer loop takes rows, the inner loop takes the key–value pairs of one row.foreachی ناوەکی: دەرەوە وەردەگرێت، ناوەوە یەک .CêIT
Example 1: every key and value of every userنموونەی 1: هەموو کلیلێک و نرخێکی هەموو بەکارهێنەرێکPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8

Outer loop: $user is one whole user (an associative array).

دەرەوە: $user یەک بەکارهێنەری تەواوە ().

Lineدێڕی 9-10

Inner loop: $key is "name" or "age", $value is 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:

Example 2: the row index and named valuesنموونەی 2: ئیندێکسی ڕیز و نرخەکانی ناودارPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8

$index is 0, 1, 2 and $user is the row at that index.

$index دەکاتە 0، 1، 2 و $user ئەو کە لەو .

Lineدێڕی 9

$index + 1 turns 0, 1, 2 into the friendlier 1, 2, 3. The brackets make PHP add before joining strings.

$index + 1 0، 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]:

هەمان بیرۆکە بە و کار دەکات. لەم نموونەی خوێندکارێکدا هەر [ناوی یەکەم، ناوی کۆتایی، تەمەن]ـە:

Students printed with for (names changed)خوێندکاران بە 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>";
}
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 + 1 shows 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⁩»؟

Quick checkپشکنینی خێرا$mark = ["Aram" => ["PHP" => 50], "Shilan" => ["PHP" => 60]]; Which loop prints "Aram: 50" and "Shilan: 60"?$mark = ["Aram" => ["PHP" => 50], "Shilan" => ["PHP" => 60]]; کام «⁦Aram: 50⁩» و «⁦Shilan: 60⁩» چاپ دەکات؟

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:

هەروەها ڕێگەت پێدەدەن بۆ هەر شتێک حیساب بکەیت، بۆ نموونە کۆی نمرەکان و باشترین نمرەی خوێندکارێک:

A total for each rowکۆکە بۆ هەر ڕیزێکPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
}
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> بۆ هەر . تێبینییەکانی خوێندکارێک ئەمە بە هەردوو جۆری پیشان دەدەن (ناوەکان و زانیارییەکان لێرەدا گۆڕدراون):

Version 1: foreachوەشانی 1: 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>";
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.

دوای دابخە.

Version 2: for with count()وەشانی 2: 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>";
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] … وەک بەکاردەهێنێت.

Quick checkپشکنینی خێراWhen you print a 2D array as an HTML table, which tag do you print once for every row of the array?کاتێک وەک HTML چاپ دەکەیت، کام بۆ هەر جارێک چاپ دەکەیت؟

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ی ناوەکی بەکاربهێنە:

A marks table with nested 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>";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 10

Outer loop: one <tr> per row.

دەرەوە: بۆ هەر <tr>ـێک.

Lineدێڕی 12-14

Inner loop: one <td> per value.

ناوەوە: بۆ هەر <td>ـێک.

Common mistakesهەڵە باوەکان

  • <= with count() in a for loop: one index too far, Undefined array key. Use <, or foreach.
  • Swapping the indexes: $cars[2][1] (row 2, column 1) is not $cars[1][2]. Row first, then column.
  • echo of a row (echo $cars[1];): prints Array with a warning. Use a second index, an inner loop or print_r().
  • Hard-coded sizes ($row < 4): break when rows are added. Use count($cars) and count($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 50 and 60 look like one number, 5060.
  • Losing the outer key: foreach ($mark as $m) drops the names; use foreach ($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 value1 as 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ڕاهێنانەکان

ExerciseڕاهێنانLab 4, Example 9تاقیگەی 4، نموونەی 9

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وەڵامەکە پیشان بدە
Solution (using count())چارەسەر (بە 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>";
}

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 ڕێک هەمان دەدات.

ExerciseڕاهێنانRow and column numbersژمارەی ڕیز و ستوون

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وەڵامەکە پیشان بدە
Solutionچارەسەر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>";
}
ExerciseڕاهێنانFind the off-by-one bugهەڵەی یەکێک زیاتر یان کەمتر بدۆزەرەوە

Fix the bug: this loop prints an empty bullet and a warning. Why, and what is the fix?

چاک بکە: ئەم خاڵێکی بەتاڵ و چاپ دەکات. بۆچی و چارەسەرەکەی چییە؟

PHP
<?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) بەکاربهێنە.

ExerciseڕاهێنانShops and pricesدوکان و نرخ

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وەڵامەکە پیشان بدە
SolutionچارەسەرPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
    }
}

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_RECURSIVE counts 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. foreach cannot go out of range.
  • Rows can be associative arrays: use foreach ($rows as $index => $row) and $row["name"], or an inner foreach ($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>ـێک.
Review questionsپرسیارەکانی پێداچوونەوە
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

PHP Tutorial (& MySQL) #8 - Multidimensional ArraysNet Ninja · Englishئینگلیزی · 105K viewsبینین · 1.6K likesلایک · Partبەشی 0:00–8:30 (ofلە 10:06)

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Sorting Arraysڕیزکردنی ڕیزەکەکان

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.