Chapterبەش 7 · Lectureوانە 7.4
Sorting Arraysڕیزکردنی ڕیزەکەکان
Put arrays in order with PHP's six sort functions. Learn what sort, rsort, asort, arsort, ksort and krsort do, a simple way to remember their names, which ones keep the keys, why sorting changes the original array, and how mixed values and capital letters are ordered.
بە شەش PHP ڕێک بخە. فێر ببە sort، rsort، asort، arsort، ksort و krsort چی دەکەن، ڕێگایەکی ئاسان بۆ لەبەرکردنی ناوەکانیان، کامیان دەپارێزن، بۆچی ڕەسەنەکە دەگۆڕێت و تێکەڵ و پیتی گەورە چۆن ڕیز دەکرێن.
What you will learnچی فێر دەبیت
- Explain ascending and descending order.
- Use
sort()andrsort()on indexed arrays. - Use
asort(),arsort(),ksort()andkrsort()on associative arrays, keeping the keys. - Remember the function names with the letters k, a and r.
- Know that sort functions change the array in place and return
true. - Predict the order of mixed values, capital letters and numeric strings.
- و ڕوون بکەیتەوە.
sort()وrsort()لەسەر بەکاربهێنیت.asort()،arsort()،ksort()وkrsort()لەسەر بەکاربهێنیت و بپارێزیت.- ناوی بە یارمەتیی پیتەکانی k، a و r لەبەر بکەیت.
- بزانیت لە شوێنی خۆیدا دەگۆڕن و
trueدەگەڕێننەوە. - ڕیزبەندیی تێکەڵ، پیتی گەورە و ژمارەیی پێشبینی بکەیت.
Why sort an array?بۆچی ڕیزەکێک ڕیز دەکەین؟
Lists are easier to read when they are in order: names from A to Z, prices from cheapest to most expensive, marks from highest to lowest. Putting the elements of an array in order is called sorting. PHP has built-in functions that sort an array for you.
لیستەکان کاتێک ڕێکخراون ئاسانتر دەخوێنرێنەوە: ناوەکان لە A تا Z، نرخی کاڵاکان لە هەرزانترینەوە بۆ گرانترین، نمرەکان لە بەرزترینەوە بۆ نزمترین. ڕێکخستنی بە پێی ڕیزبەندییەک پێی دەوترێت . PHP چەند هەیە کە بۆ ڕیز دەکەن.
Putting items in a fixed order, for example numbers from the smallest to the largest (), the newest files first () or names in alphabetical order. A , a and many other programs can sort .
دانانی شتەکانە بە ڕیزبەندییەکی دیاریکراو، بۆ نموونە ژمارەکان لە بچووکترینەوە بۆ گەورەترین ()، نوێترین فایلەکان لە سەرەتادا () یان ناوەکان بەپێی ئەلفوبێ. ، و زۆر بەرنامەی تر دەتوانن ڕیز بکەن.
- Ascending order: from small to big: 1, 2, 3 … or A to Z.
- Descending order: from big to small: 3, 2, 1 … or Z to A.
- : لە بچووکەوە بۆ گەورە: 1، 2، 3 … یان A تا Z.
- : لە گەورەوە بۆ بچووک: 3، 2، 1 … یان Z تا A.
Imagine students lining up for a class photo, shortest first. If every student wears a name badge, the badges move with the students: you can still tell who is who. That is asort(). If instead the photographer takes the badges away and gives the students new numbers 1, 2, 3 by position, you lose the names. That is sort().
وێنای بکە خوێندکاران بۆ وێنەی پۆل ڕیز دەبەستن، کورتترین یەکەم. ئەگەر هەر خوێندکارێک کارتی ناوی پێوە بێت، کارتەکان لەگەڵ خوێندکاران دەجوڵێن: هێشتا دەزانیت کێ کێیە. ئەمە asort()ـە. بەڵام ئەگەر وێنەگرەکە کارتەکان لێ بکاتەوە و بەپێی شوێن ژمارەی نوێی 1، 2، 3 بداتە خوێندکاران، ناوەکان ون دەبن. ئەمە sort()ـە.
Where the analogy stops workingسنووری ئەم نموونەیە
In the photo line the students physically move. A PHP sort function rearranges the same array in memory; it does not make a new copy.
لە ڕیزی وێنەکەدا خوێندکاران بە جەستە دەجوڵێن. PHP هەمان لە ڕێک دەخاتەوە؛ کۆپییەکی نوێ دروست ناکات.
The six sort functionsشەش کردارەکەی ڕیزکردن
The lab, the lecture summary and the course notes all give the same list of six functions:
تاقیگە، کورتەی وانەکان و تێبینییەکانی کۆرسەکە هەموویان هەمان لیستی شەش دەدەن:
| Function | Description in the courseوەسف لە کۆرسەکەدا | Sorts byبەپێی چی ڕیز دەکات | Orderڕیزبەندی | Keys after sorting دوای |
|---|---|---|---|---|
sort() | sort arrays in ascending order بە ڕیز دەکات | value | ascendingبەرزبوونەوە | new keys 0, 1, 2 … نوێ 0، 1، 2 … |
rsort() | sort arrays in descending order بە ڕیز دەکات | value | descendingنزمبوونەوە | new keys 0, 1, 2 … نوێ 0، 1، 2 … |
asort() | sort associative arrays in ascending order, according to the value بە ڕیز دەکات، بەپێی | value | ascendingبەرزبوونەوە | keptدەمێننەوە |
arsort() | sort associative arrays in descending order, according to the value بە ڕیز دەکات، بەپێی | value | descendingنزمبوونەوە | keptدەمێننەوە |
ksort() | sort associative arrays in ascending order, according to the key بە ڕیز دەکات، بەپێی | key | ascendingبەرزبوونەوە | keptدەمێننەوە |
krsort() | sort associative arrays in descending order, according to the key بە ڕیز دەکات، بەپێی | key | descendingنزمبوونەوە | keptدەمێننەوە |
Three facts that the course list does not say, but that you must know:
سێ ڕاستی کە لیستەکەی کۆرسەکە نایڵێت، بەڵام دەبێت بیانزانیت:
- They sort the array in place. The function changes the array you give it. It does not return a new sorted array; it returns
true. (See sorting changes the original.) sort()andrsort()throw the keys away and renumber the elements 0, 1, 2 … Use them for indexed arrays (lists).asort(),arsort(),ksort()andkrsort()keep every key with its value. Use them for associative arrays.
- لە شوێنی خۆیدا ڕیز دەکەن. ئەو دەگۆڕێت کە پێی دەدەیت. نوێی ڕیزکراو ناگەڕێنێتەوە؛
trueدەگەڕێنێتەوە. (بڕوانە ڕیزکردن ڕیزەکە ڕەسەنەکە دەگۆڕێت.) sort()وrsort()فڕێ دەدەن و لە نوێوە بە 0، 1، 2 … ژمارە دەکەن. بۆ (لیستەکان) بەکاریان بهێنە.asort()،arsort()،ksort()وkrsort()هەر لەگەڵ دەهێڵنەوە. بۆ بەکاریان بهێنە.
The six sort functionsشەش کردارەکەی ڕیزکردن
All six on the same arrayهەر شەشیان لەسەر هەمان ڕیزەک
<?php
function show($label, $array) {
echo str_pad($label, 7) . ": ";
foreach ($array as $key => $value) {
echo "[$key] => $value ";
}
echo "\n";
}
$start = ["Rezan" => 22, "Aram" => 19, "Shilan" => 21];
echo "<pre>";
$a = $start; sort($a); show("sort", $a);
$a = $start; rsort($a); show("rsort", $a);
$a = $start; asort($a); show("asort", $a);
$a = $start; arsort($a); show("arsort", $a);
$a = $start; ksort($a); show("ksort", $a);
$a = $start; krsort($a); show("krsort", $a);
echo "</pre>";sort : [0] => 19 [1] => 21 [2] => 22 rsort : [0] => 22 [1] => 21 [2] => 19 asort : [Aram] => 19 [Shilan] => 21 [Rezan] => 22 arsort : [Rezan] => 22 [Shilan] => 21 [Aram] => 19 ksort : [Aram] => 19 [Rezan] => 22 [Shilan] => 21 krsort : [Shilan] => 21 [Rezan] => 22 [Aram] => 19
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>sort : [0] => 19 [1] => 21 [2] => 22
rsort : [0] => 22 [1] => 21 [2] => 19
asort : [Aram] => 19 [Shilan] => 21 [Rezan] => 22
arsort : [Rezan] => 22 [Shilan] => 21 [Aram] => 19
ksort : [Aram] => 19 [Rezan] => 22 [Shilan] => 21
krsort : [Shilan] => 21 [Rezan] => 22 [Aram] => 19
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-8
A small helper function prints one line: the label, then every
[key] => value.یارمەتیدەری بچووک یەک دێڕ چاپ دەکات: ناونیشانەکە، پاشان هەموو
[key] => valueـێک.- Lineدێڕی 13
$a = $start;makes a fresh copy, so every function starts from the same unsorted array.$a = $start;کۆپییەکی نوێ دروست دەکات، بۆیە هەموو لە هەمان ڕیزنەکراوەوە دەست پێدەکات.- Lineدێڕی 13-18
Compare the keys:
sortandrsortshow 0, 1, 2; the other four keep the names.بەراورد بکە:
sortوrsort0، 1، 2 پیشان دەدەن؛ چوارەکەی تر ناوەکان دەهێڵنەوە.
Remembering the namesلەبەرکردنی ناوەکان
The names look alike, but they are built from small letters added before sort. The lecture summary underlines the letters and gives the memory aid "sort KAR KR AR": sort, ksort, asort, rsort, krsort, arsort.
ناوەکان لە یەک دەچن، بەڵام لە چەند پیتێکی بچووک دروست بوون کە پێش sort زیاد دەکرێن. کورتەی وانەکان پیتەکان هێڵیان بەژێردا دەکێشێت و ڕێگایەکی لەبەرکردن دەدات، «sort KAR KR AR»: sort، ksort، asort، rsort، krsort، arsort.
- k = key: sort by the keys.
- a = associative: sort by the values, but keep each key attached to its value (for associative arrays).
- r = reverse: descending order, from big to small.
- no letter:
sort()sorts by value in ascending order and gives new keys 0, 1, 2 …
- k = key (): بەپێی ڕیز بکە.
- a = associative (پەیوەندیدار): بەپێی ڕیز بکە، بەڵام هەر بە بهێڵەوە (بۆ ).
- r = reverse (پێچەوانە): ، لە گەورەوە بۆ بچووک.
- بێ پیت:
sort()بەپێی بە ڕیز دەکات و نوێی 0، 1، 2 … دەدات.
sort() and rsort(): sorting listssort() و rsort(): ڕیزکردنی لیستەکان
sort($array) puts the values in ascending order; rsort($array) puts them in descending order. Both give the elements new indexes 0, 1, 2 …, which makes them perfect for indexed arrays. The lab prints the sorted arrays with a for loop and count().
sort($array) بە ڕیز دەکات؛ rsort($array) بە . هەردووکیان نوێی 0، 1، 2 … دەدەنە ، کە وایان لێدەکات بۆ تەواو گونجاو بن. تاقیگەکە ڕیزکراو بە و count() چاپ دەکات.
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}BMW Toyota Volvo
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
BMW<br>Toyota<br>Volvo<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
sort()changes$carsitself: BMW, Toyota, Volvo.sort()خودی$carsدەگۆڕێت: BMW، Toyota، Volvo.- Lineدێڕی 4
count()returns 3, the number of elements.count()3 دەگەڕێنێتەوە، واتە ژمارەی .- Lineدێڕی 5-8
Print index 0, 1, 2 of the sorted array.
0، 1، 2ی ڕیزکراوەکە چاپ بکە.
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}2 4 6 11 22
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
2<br>4<br>6<br>11<br>22<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Numbers are compared as numbers, so 11 comes after 6 (not after 2, as it would in a dictionary).
ژمارەکان وەک ژمارە بەراورد دەکرێن، بۆیە 11 دوای 6 دێت (نەک دوای 2 وەک لە فەرهەنگدا).
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}Volvo Toyota BMW
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Volvo<br>Toyota<br>BMW<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
rsort(): the same assort(), but from Z to A.rsort(): وەکsort()، بەڵام لە Z بۆ A.
asort() and arsort(): by value, keeping the keysasort() و arsort(): بەپێی نرخ، بە پاراستنی کلیلەکان
asort() sorts an associative array by its values in ascending order; arsort() does the same in descending order. Every key moves together with its value. The lab prints the result with foreach ($age as $x => $x_value), which gives each key in $x and each value in $x_value.
asort() بەپێی بە ڕیز دەکات؛ arsort() هەمان کار بە دەکات. هەر لەگەڵ پێکەوە دەجوڵێن. تاقیگەکە ئەنجامەکە بە foreach ($age as $x => $x_value) چاپ دەکات، کە هەر دەخاتە $x و هەر دەخاتە $x_value.
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
foreach ($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Key=Peter, Value=35<br>Key=Ben, Value=37<br>Key=Joe, Value=43<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Name => age. The ages are written as strings ("35"); numbers (35) would be more natural, and sort the same way here.
ناو => تەمەن. تەمەنەکان وەک نووسراون ("35")؛ ژمارە (35) سروشتیتر دەبوو و لێرەدا بە هەمان شێوە ڕیز دەکرێت.
- Lineدێڕی 3
asort()sorts by the values 35, 37, 43 and keeps the names.asort()بەپێی 35، 37، 43 ڕیز دەکات و ناوەکان دەهێڵێتەوە.- Lineدێڕی 5-8
$xis the key (name),$x_valuethe value (age).$x(ناو)،$x_value(تەمەن).
asort() کاتێک ڕیزبەندییەکە دەگۆڕێتPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$age = ["Rezan" => 22, "Aram" => 19, "Shilan" => 21];
asort($age); // by value, smallest first, keys stay with their values
foreach ($age as $name => $years) {
echo "$name is $years<br>";
}Aram is 19 Shilan is 21 Rezan is 22
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Aram is 19<br>Shilan is 21<br>Rezan is 22<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Unsorted: Rezan 22, Aram 19, Shilan 21.
ڕیزنەکراو: Rezan 22، Aram 19، Shilan 21.
- Lineدێڕی 4
Smallest value first; each name stays with its age.
بچووکترین یەکەم؛ هەر ناوێک لەگەڵ تەمەنەکەی دەمێنێتەوە.
arsort(): بەپێی نرخ، نزمبوونەوەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$age = ["Rezan" => 22, "Aram" => 19, "Shilan" => 21];
arsort($age); // by value, largest first, keys stay with their values
foreach ($age as $name => $years) {
echo "$name is $years<br>";
}Rezan is 22 Shilan is 21 Aram is 19
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Rezan is 22<br>Shilan is 21<br>Aram is 19<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
Largest value first.
گەورەترین یەکەم.
ksort() and krsort(): by keyksort() و krsort(): بەپێی کلیل
ksort() sorts an associative array by its keys in ascending order (A to Z, small to big); krsort() sorts by the keys in descending order. Again every value stays with its key.
ksort() بەپێی بە ڕیز دەکات (A تا Z، بچووک بۆ گەورە)؛ krsort() بەپێی بە ڕیز دەکات. دیسان هەر لەگەڵ دەمێنێتەوە.
Lab 4, Example 14 is titled "Sort associative arrays in ascending order, according to the key", but its code calls krsort(), which sorts the keys in descending order:
تاقیگەی 4، نموونەی 14 ناونیشانی « بە ، بەپێی »ـە، بەڵام کۆدەکەی krsort() بانگ دەکات، کە بە ڕیز دەکات:
krsort()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
foreach ($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}Key=Peter, Value=35 Key=Joe, Value=43 Key=Ben, Value=37
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Key=Peter, Value=35<br>Key=Joe, Value=43<br>Key=Ben, Value=37<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Keys in descending order: Peter, Joe, Ben (P > J > B).
بە : Peter، Joe، Ben (P > J > B).
ksort()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age); // ascending by key: this matches the lab's heading
foreach ($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}Key=Ben, Value=37 Key=Joe, Value=43 Key=Peter, Value=35
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Key=Ben, Value=37<br>Key=Joe, Value=43<br>Key=Peter, Value=35<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Keys in ascending order: Ben, Joe, Peter.
بە : Ben، Joe، Peter.
Sorting changes the original arrayڕیزکردن ڕیزەکە ڕەسەنەکە دەگۆڕێت
All six functions work in place: they rearrange the array variable you pass to them, and they return only true. A common mistake is to expect a sorted array back:
هەر شەش لە شوێنی خۆیدا کار دەکەن: ئەو ڕێک دەخەنەوە کە پێیانی دەدەیت و تەنها true دەگەڕێننەوە. باو ئەوەیە چاوەڕێی گەڕانەوەی ڕیزکراو بکەیت:
sort() true دەگەڕێنێتەوە، نەک ڕیزەکPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$numbers = [4, 6, 2];
$sorted = sort($numbers); // sort() changes $numbers itself
var_dump($sorted); // bool(true): not a sorted array!
echo implode(", ", $numbers); // the original array is now sortedbool(true) 2, 4, 6
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
bool(true)
2, 4, 6Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
$sortedreceivestrue.$numbersitself is now sorted.$sortedtrueوەردەگرێت. خودی$numbersئێستا ڕیز کراوە.- Lineدێڕی 6
implode(", ", …)joins the values with commas.implode(", ", …)بە کۆما پێکەوە دەلکێنێت.
If you still need the original order, copy the array first and sort the copy. Assigning an array to another variable makes a copy:
ئەگەر هێشتا ڕیزبەندییە ڕەسەنەکەت پێویستە، سەرەتا کۆپی بکە و کۆپییەکە ڕیز بکە. دانانی بۆ تر کۆپییەک دروست دەکات:
<?php
$numbers = [4, 6, 2];
$sorted = $numbers; // 1. copy the array
sort($sorted); // 2. sort the copy
echo "Original: " . implode(", ", $numbers) . "<br>";
echo "Sorted: " . implode(", ", $sorted);Original: 4, 6, 2 Sorted: 2, 4, 6
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Original: 4, 6, 2<br>Sorted: 2, 4, 6Mixed values, capital letters and number stringsنرخەکانی تێکەڵ، پیتی گەورە و ژمارە لەناو زنجیرەنووسەدا
Sorting compares values two at a time. Usually that is simple, but three cases can surprise you.
هەر جارە دوو بەراورد دەکات. زۆرجار ئەمە سادەیە، بەڵام سێ حاڵەت دەتوانن سەرت سوڕبهێنن.
Mixed types
جۆری تێکەڵ
PHP 8 compares a number and a numeric string as numbers, but a number and a non-numeric string as text. Text is compared character by character, and digits come before letters. So in the homework below, "1" < 22, then 22 < "Aram Karim", then "Aram Karim" < "IT":
PHP 8 ژمارە و ژمارەیی وەک ژمارە بەراورد دەکات، بەڵام ژمارە و ناژمارەیی وەک دەق. دەق پیت بە پیت بەراورد دەکرێت و ژمارەکان پێش پیتەکان دێن. بۆیە لە ئەرکی ماڵەوەی خوارەوەدا، "1" < 22، پاشان 22 < "Aram Karim"، پاشان "Aram Karim" < "IT":
<?php
var_dump("1" < 22); // true: both are numbers
var_dump(22 < "Aram Karim"); // true: compared as text, "2" comes before "A"
var_dump("Aram Karim" < "IT"); // true: "A" comes before "I"bool(true) bool(true) bool(true)
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
A numeric string and a number: compared as numbers, 1 < 22.
ژمارەیی و ژمارە: وەک ژمارە بەراورد دەکرێن، 1 < 22.
- Lineدێڕی 3
A number and a non-numeric string: compared as text, "22" < "Aram Karim" because "2" comes before "A".
ژمارە و ناژمارەیی: وەک دەق بەراورد دەکرێن، "22" < "Aram Karim" چونکە "2" پێش "A" دێت.
Mixed values, capital letters and number stringsنرخەکانی تێکەڵ، پیتی گەورە و ژمارە لەناو زنجیرەنووسەدا
Capital lettersپیتی گەورە
Normal string sorting is case-sensitive: all capital letters come before all small letters. To ignore case, pass the flags SORT_STRING | SORT_FLAG_CASE:
ئاسایی : هەموو پیتە گەورەکان پێش هەموو پیتە بچووکەکان دێن. بۆ پشتگوێخستنی قەبارەی پیت، SORT_STRING | SORT_FLAG_CASE بدە:
<?php
$fruits = ["banana", "apple", "Cherry"];
sort($fruits);
echo implode(", ", $fruits) . "<br>"; // capital letters come first
sort($fruits, SORT_STRING | SORT_FLAG_CASE);
echo implode(", ", $fruits); // ignore upper and lower caseCherry, apple, banana apple, banana, Cherry
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Cherry, apple, banana<br>apple, banana, CherryMixed values, capital letters and number stringsنرخەکانی تێکەڵ، پیتی گەورە و ژمارە لەناو زنجیرەنووسەدا
Numbers stored as stringsژمارەی هەڵگیراو وەک زنجیرەنووسە
Strings that look like numbers ("10", "9", "100") are compared as numbers by default. If you want dictionary order instead, use SORT_STRING:
کە وەک ژمارە دەردەکەون ("10"، "9"، "100") بە شێوەی بنەڕەتی وەک ژمارە بەراورد دەکرێن. ئەگەر ڕیزبەندیی فەرهەنگیت دەوێت، SORT_STRING بەکاربهێنە:
<?php
$codes = ["10", "9", "100"];
sort($codes); // numeric strings are compared as numbers
echo implode(", ", $codes) . "<br>";
sort($codes, SORT_STRING); // compare them as text, letter by letter
echo implode(", ", $codes);9, 10, 100 10, 100, 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
9, 10, 100<br>10, 100, 9Worked homework: sorting the personal-information arrayئەرکی ماڵەوەی چارەسەرکراو: ڕیزکردنی ڕیزەکی زانیاریی کەسی
Questions 4 and 5 of the associative-array homework (questions 1–3 are in Arrays in PHP) sort the personal-information array. The question texts were not saved; they are rebuilt here from the answers, with neutral data.
پرسیاری 4 و 5ی ئەرکی ماڵەوەی (پرسیاری 1–3 لە ڕیزەکەکان لە PHPـدادان) زانیاریی کەسی ڕیز دەکەن. دەقی پرسیارەکان پاشەکەوت نەکراون؛ لێرەدا لە چارەسەرەکانەوە دووبارە دروست کراونەتەوە، بە زانیاریی بێلایەن.
asort()Sort the personal-information array (ID, Name, Age, Department, City) in ascending order by value, keeping the keys, and print every key and value.
زانیاریی کەسی (ID، Name، Age، Department، City) بە بەپێی ڕیز بکە، بە پاراستنی و هەموو و چاپ بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$personalInformation = array(
'ID' => "1",
'Name' => "Aram Karim",
'Age' => 22,
'Department' => "IT",
'City' => "Slemani"
);
echo "Question 4's Answer: <br />";
asort($personalInformation);
foreach ($personalInformation as $key => $value) {
echo $key . ": " . $value . " — ";
}Question 4's Answer: ID: 1 — Age: 22 — Name: Aram Karim — Department: IT — City: Slemani —
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Question 4's Answer: <br />ID: 1 — Age: 22 — Name: Aram Karim — Department: IT — City: Slemani —Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 11
asort(): by value, ascending, keys kept.asort(): بەپێی ، بەرزبوونەوە، دەمێننەوە.
Why this order? The values are mixed: the string "1", the integer 22 and three non-numeric strings. As shown above, "1" < 22 as numbers, 22 < "Aram Karim" as text (digits before letters), and then the strings in alphabetical order: "Aram Karim" < "IT" < "Slemani". So "from small to big" only partly describes the result. The student also wrote the function name as "absort()" in the comment; the real name is asort().
بۆچی ئەم ڕیزبەندییە؟ تێکەڵن: "1"، 22 و سێ ناژمارەیی. وەک لە سەرەوە پیشان درا، "1" < 22 وەک ژمارە، 22 < "Aram Karim" وەک دەق (ژمارەکان پێش پیتەکان)، پاشان بە ڕیزی ئەلفوبێ: "Aram Karim" < "IT" < "Slemani". بۆیە «لە بچووکەوە بۆ گەورە» تەنها بەشێک لە ئەنجامەکە وەسف دەکات. خوێندکارەکە لە ناوی بە «absort()» نووسیبوو؛ ناوە ڕاستەکەی asort()ـە.
krsort()Sort the same array in descending order by key and print it.
هەمان بە بەپێی ڕیز بکە و چاپی بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$personalInformation = array(
'ID' => "1",
'Name' => "Aram Karim",
'Age' => 22,
'Department' => "IT",
'City' => "Slemani"
);
echo "Question 5's Answer: <br />";
krsort($personalInformation);
foreach ($personalInformation as $key => $value) {
echo $key . ": " . $value . " — ";
}Question 5's Answer: Name: Aram Karim — ID: 1 — Department: IT — City: Slemani — Age: 22 —
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Question 5's Answer: <br />Name: Aram Karim — ID: 1 — Department: IT — City: Slemani — Age: 22 —Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 11
krsort(): by key, descending. N > I > D > C > A, so Name comes first and Age last.krsort(): بەپێی ، نزمبوونەوە. N > I > D > C > A، بۆیە Name یەکەم دێت و Age کۆتایی.
The student explained: the r in krsort() means reverse and the k means key, so the sorting is done by the keys. "N" comes after "I", "D", "C" and "A", which is why "Name" is printed first, then "ID", and so on. In the original file this question sorted the array that question 4 had already sorted; with krsort() the result is the same either way, because it only looks at the keys.
خوێندکارەکە ڕوونی کردەوە: r لە krsort()دا واتە reverse (پێچەوانە) و k واتە key، بۆیە بەپێی دەکرێت. «N» دوای «I»، «D»، «C» و «A» دێت، هەر بۆیە «Name» یەکەم چاپ دەکرێت، پاشان «ID» و بەم شێوەیە. لە فایلە ڕەسەنەکەدا ئەم پرسیارە ئەو ڕیز دەکرد کە پرسیاری 4 پێشتر ڕیزی کردبوو؛ بە krsort() ئەنجامەکە هەر وەک یەکە، چونکە تەنها سەیری دەکات.
Common mistakesهەڵە باوەکان
sort()on an associative array: the keys are replaced by 0, 1, 2. Useasort()/arsort()(by value) orksort()/krsort()(by key).$sorted = sort($a);:$sortedbecomestrue. Sort functions change$aitself.- Thinking the
ainasortmeans "ascending": it means associative (keys kept). - Choosing
krsort()when you want ascending keys: useksort(). Theralways means reverse (descending). - Forgetting that the original order is lost: copy the array first if you need it later.
- Surprised by capital letters or mixed types: "Cherry" comes before "apple"; numbers come before letters. Use sort flags or keep one type per array.
- Using a
forloop to print a sorted associative array: its keys are not 0, 1, 2. Useforeach ($a as $key => $value).
sort()لەسەر : بە 0، 1، 2 دەگۆڕدرێن.asort()/arsort()(بەپێی ) یانksort()/krsort()(بەپێی ) بەکاربهێنە.$sorted = sort($a);:$sortedدەبێتەtrue. خودی$aدەگۆڕن.- وا زانین کە
aلەasortدا واتە «ascending»: واتە associative ( دەمێننەوە). - هەڵبژاردنی
krsort()کاتێک بەرزبوونەوەت دەوێت:ksort()بەکاربهێنە.rهەمیشە واتە پێچەوانە (نزمبوونەوە). - لەبیرچوونی ئەوەی ڕیزبەندیی ڕەسەن ون دەبێت: ئەگەر دواتر پێویستت پێیەتی، سەرەتا کۆپی بکە.
- سەرسووڕمان لە پیتی گەورە یان جۆری تێکەڵ: "Cherry" پێش "apple" دێت؛ ژمارەکان پێش پیتەکان دێن. ئاڵاکانی بەکاربهێنە یان لە هەر یەک جۆر دابنێ.
- بەکارهێنانی بۆ چاپکردنی ڕیزکراو: 0، 1، 2 نین.
foreach ($a as $key => $value)بەکاربهێنە.
Exercisesڕاهێنانەکان
Lab 4, Examples 10–14تاقیگەی 4، نموونەکانی 10–14
Lab 4, Example 10: sort the array ("Volvo", "BMW", "Toyota") alphabetically and print each car on its own line using count() and a for loop.
تاقیگەی 4، نموونەی 10: ("Volvo"، "BMW"، "Toyota") بەپێی ئەلفوبێ ڕیز بکە و هەر ئۆتۆمبێلێک لە دێڕێکی جیادا بە count() و چاپ بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}BMW Toyota Volvo
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
BMW<br>Toyota<br>Volvo<br>Lab 4, Example 11: sort the numbers (4, 6, 2, 22, 11) in ascending order and print them.
تاقیگەی 4، نموونەی 11: ژمارەکانی (4، 6، 2، 22، 11) بە ڕیز بکە و چاپیان بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}2 4 6 11 22
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
2<br>4<br>6<br>11<br>22<br>Lab 4, Example 12: print the same cars in descending alphabetical order.
تاقیگەی 4، نموونەی 12: هەمان ئۆتۆمبێلەکان بە ڕیزبەندیی ئەلفوبێیی پێچەوانە (لە Z بۆ A) چاپ بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}Volvo Toyota BMW
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Volvo<br>Toyota<br>BMW<br>Lab 4, Example 13: sort the associative array ("Peter" => "35", "Ben" => "37", "Joe" => "43") in ascending order according to the value, and print "Key=…, Value=…" for each element.
تاقیگەی 4، نموونەی 13: ("Peter" => "35"، "Ben" => "37"، "Joe" => "43") بە بەپێی ڕیز بکە و بۆ هەر «Key=…, Value=…» چاپ بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
foreach ($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Key=Peter, Value=35<br>Key=Ben, Value=37<br>Key=Joe, Value=43<br>Lab 4, Example 14: sort the same array in ascending order according to the key. (The lab's code uses krsort(); what does it print, and which function matches the task?)
تاقیگەی 4، نموونەی 14: هەمان بە بەپێی ڕیز بکە. (کۆدی تاقیگەکە krsort() بەکاردەهێنێت؛ چی چاپ دەکات و کام لەگەڵ ئەرکەکە دەگونجێت؟)
Show answerوەڵامەکە پیشان بدە
ksort()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age); // ascending by key: this matches the lab's heading
foreach ($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}Key=Ben, Value=37 Key=Joe, Value=43 Key=Peter, Value=35
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Key=Ben, Value=37<br>Key=Joe, Value=43<br>Key=Peter, Value=35<br>The lab's krsort() prints Peter, Joe, Ben (descending keys). The task asks for ascending keys, which is ksort(): Ben, Joe, Peter.
krsort()ـی تاقیگەکە Peter، Joe، Ben چاپ دەکات ( نزمبوونەوە). ئەرکەکە داوای بەرزبوونەوە دەکات، کە ksort()ـە: Ben، Joe، Peter.
Exercisesڕاهێنانەکان
More practiceڕاهێنانی زیاتر
Store five names in an array. Print them from A to Z, then from Z to A.
پێنج ناو لە هەڵبگرە. لە A تا Z چاپیان بکە، پاشان لە Z تا A.
Show answerوەڵامەکە پیشان بدە
<?php
$names = ["Shilan", "Aram", "Rezan", "Dilan", "Karwan"];
sort($names);
echo "A to Z: " . implode(", ", $names) . "<br>";
rsort($names);
echo "Z to A: " . implode(", ", $names);A to Z: Aram, Dilan, Karwan, Rezan, Shilan Z to A: Shilan, Rezan, Karwan, Dilan, Aram
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
A to Z: Aram, Dilan, Karwan, Rezan, Shilan<br>Z to A: Shilan, Rezan, Karwan, Dilan, AramA shop stores item => price. Print the items from cheapest to most expensive, then in alphabetical order of the item names. Keep each item with its price.
دوکانێک کاڵا => نرخ هەڵدەگرێت. کاڵاکان لە هەرزانترینەوە بۆ گرانترین چاپ بکە، پاشان بە ڕیزی ئەلفوبێی ناوی کاڵاکان. هەر کاڵایەک لەگەڵ نرخەکەی بهێڵەوە.
Hintئاماژە
By value with the keys kept: asort(). By key: ksort().
بەپێی و پاراستنی : asort(). بەپێی : ksort().
Show answerوەڵامەکە پیشان بدە
<?php
$prices = ["Tea" => 1000, "Rice" => 3000, "Sugar" => 2500, "Bread" => 500];
asort($prices);
echo "Cheapest first:<br>";
foreach ($prices as $item => $price) {
echo "$item: $price<br>";
}
ksort($prices);
echo "By name:<br>";
foreach ($prices as $item => $price) {
echo "$item: $price<br>";
}Cheapest first: Bread: 500 Tea: 1000 Sugar: 2500 Rice: 3000 By name: Bread: 500 Rice: 3000 Sugar: 2500 Tea: 1000
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Cheapest first:<br>Bread: 500<br>Tea: 1000<br>Sugar: 2500<br>Rice: 3000<br>By name:<br>Bread: 500<br>Rice: 3000<br>Sugar: 2500<br>Tea: 1000<br>Summary and reviewکورتە و پێداچوونەوە
- Six functions sort arrays:
sort,rsort,asort,arsort,ksort,krsort. - Letters: k = by key, a = associative (by value, keys kept), r = reverse (descending). No
k/a: by value with new keys. - Use
sort/rsortfor indexed lists;asort/arsort/ksort/krsortfor associative arrays. - All six sort in place and return
true; copy first if you need the original order. - Numbers sort numerically; strings sort character by character, capital letters first; mixed types follow PHP 8 comparison rules.
- Print a sorted associative array with
foreach ($a as $key => $value).
- شەش ڕیز دەکەن:
sort،rsort،asort،arsort،ksort،krsort. - پیتەکان: k = بەپێی ، a = پەیوەندیدار (بەپێی ، دەمێننەوە)، r = پێچەوانە (نزمبوونەوە). بێ
k/a: بەپێی بە نوێ. sort/rsortبۆ ؛asort/arsort/ksort/krsortبۆ .- هەر شەشیان لە شوێنی خۆیدا ڕیز دەکەن و
trueدەگەڕێننەوە؛ ئەگەر ڕیزبەندیی ڕەسەنت پێویستە سەرەتا کۆپی بکە. - ژمارەکان وەک ژمارە ڕیز دەکرێن؛ پیت بە پیت، پیتە گەورەکان یەکەم؛ جۆری تێکەڵ پەیڕەوی یاساکانی بەراوردکردنی PHP 8 دەکات.
- ڕیزکراو بە
foreach ($a as $key => $value)چاپ بکە.
What is the difference between asort() and ksort()?جیاوازیی نێوان asort() و ksort() چییە؟
asort() sorts by the values, ksort() by the keys. Both keep each key with its value, and both are ascending.
asort() بەپێی ڕیز دەکات، ksort() بەپێی . هەردووکیان هەر لەگەڵ دەهێڵنەوە و هەردووکیان بەرزبوونەوەن.
Why is sort() a bad choice for ["Aram" => 19, "Rezan" => 22]?بۆچی sort() هەڵبژاردنێکی خراپە بۆ ["Aram" => 19, "Rezan" => 22]؟
It replaces the names with 0 and 1, so you can no longer tell whose age is whose.
ناوەکان بە 0 و 1 دەگۆڕێت، بۆیە ئیتر نازانیت کام تەمەن هی کێیە.
After $a = [4, 6, 2]; rsort($a);, what is in $a?دوای $a = [4, 6, 2]; rsort($a);، چی لەناو $aدایە؟
[0 => 6, 1 => 4, 2 => 2]. (rsort() needs a variable: rsort([4, 6, 2]) is an error.)
[0 => 6, 1 => 4, 2 => 2]. (rsort() پێویستی بە هەیە: rsort([4, 6, 2]) .)
Lab 4, Example 14 says "ascending, according to the key" but uses krsort(). Which is right?تاقیگەی 4، نموونەی 14 دەڵێت «بەرزبوونەوە، بەپێی کلیل» بەڵام krsort() بەکاردەهێنێت. کامیان ڕاستە؟
Ascending by key is ksort(); krsort() is descending. The heading and the code disagree.
بەرزبوونەوە بەپێی ksort()ـە؛ krsort() نزمبوونەوەیە. ناونیشان و کۆدەکە یەک ناگرنەوە.
Why does "Cherry" come before "apple" after sort()?بۆچی دوای sort() "Cherry" پێش "apple" دێت؟
String sorting is case-sensitive and capital letters come before small letters. Use SORT_STRING | SORT_FLAG_CASE to ignore case.
و پیتە گەورەکان پێش پیتە بچووکەکان دێن. بۆ پشتگوێخستنی قەبارەی پیت SORT_STRING | SORT_FLAG_CASE بەکاربهێنە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
PHP Superglobalsسووپەرگلۆباڵەکانی PHP
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 ن.