The first thing that I didn't find in description it's that this function return results from MIN value to MAX value, ex: [-5=>'', 0=>'', 5=>'' ]
Also you should know that by default, it has correct sorting for keys that represented as string but has a number as value, ex: ['-5'=>'', '0'=>'', '5'=>'' ]
Few examples with results:_
-----------------------------------------
DESCRIPTION: Keys are numbers + default flag (SORT_REGULAR)_
$arr = [
-5 => 'minus five',
0 => 'zero',
1 => 'one',
2 => 'two',
100 => 'hundred',
];
ksort($arr);
print_r($arr);_
RESULT:_
Array
(
[-5] => minus five
[0] => zero
[1] => one
[2] => two
[100] => hundred
)
-----------------------------------------
DESCRIPTION: Keys are string numbers + default flag (SORT_REGULAR)
Also you should know that by default, it has correct sorting for keys that represented as string but has a number as value, ex: ['-5'=>'', '0'=>'', '5'=>'' ]0
ksort($arr);
print_r($arr);_
RESULT:_
Array
(
[-5] => minus five
[0] => zero
[1] => one
[2] => two
[100] => hundred
)
Also you should know that by default, it has correct sorting for keys that represented as string but has a number as value, ex: ['-5'=>'', '0'=>'', '5'=>'' ]_4
Also you should know that by default, it has correct sorting for keys that represented as string but has a number as value, ex: ['-5'=>'', '0'=>'', '5'=>'' ]0
Also you should know that by default, it has correct sorting for keys that represented as string but has a number as value, ex: ['-5'=>'', '0'=>'', '5'=>'' ]6
RESULT:_
Also you should know that by default, it has correct sorting for keys that represented as string but has a number as value, ex: ['-5'=>'', '0'=>'', '5'=>'' ]8
-----------------------------------------
Few examples with results:_0
Also you should know that by default, it has correct sorting for keys that represented as string but has a number as value, ex: ['-5'=>'', '0'=>'', '5'=>'' ]0
Few examples with results:_2
RESULT:_
Array
(
[-5] => minus five
[0] => zero
[1] => one
[2] => two
[100] => hundred
)
To sort an array of multiple text fields alphabetically you have to make the text lowercase before sorting the array. Otherwise PHP puts acronyms before words. You can see this in my example code. Simply store the original text field at the end of the array line and call it later from there. You can safely ignore the lowercase version which is added to the start of the array line.
echo '
ORIGINAL DATA:';
$data = array(
'Saturn|7|8|9|0||',
'Hello|0|1|2|3||',
'SFX|5|3|2|4||',
'HP|9|0|5|6||'
);
print_r($data);
sort($data);
reset($data);
echo '
RAW SORT:
';
print_r($data);
for ($c = 0; $c < count($data); $c++) {
list ($letter,$g1,$g2,$g3,$g4,$end) = explode ('|', $data[$c]);
$lowercase = strtolower($letter);
$data2[$c] = array($lowercase,$g1,$g2,$g3,$g4,$letter);
}
sort($data2);
reset($data2);
echo '
LOWERCASE SORT:
';
print_r($data2);
echo '
';?>
While this may seem obvious, user-defined array sorting functions ( uksort(), uasort(), usort() ) will *not* be called if the array does not have *at least two values in it*.
The following code:
function usortTest($a, $b) {
var_dump($a);
var_dump($b);
return -1;
}
$test = array('val1');
usort($test, "usortTest");
$test2 = array('val2', 'val3');
usort($test2, "usortTest");
?>
Will output:
string(4) "val3"
string(4) "val2"
The first array doesn't get sent to the function.
Please, under no circumstance, place any logic that modifies values, or applies non-sorting business logic in these functions as they will not always be executed.
Elemen-elemen dalam array dapat diurutkan dalam urutan abjad atau numerik, turun atau naik
PHP - Mengurutkan Fungsi Untuk Array
Dalam bab ini, kita akan membahas fungsi pengurutan array PHP berikut
- sort()_ - mengurutkan array dalam urutan menaik
- rsort()_ - mengurutkan array dalam urutan menurun
- asort() - urutkan array asosiatif dalam urutan menaik, sesuai dengan nilainya
- ksort() - urutkan array asosiatif dalam urutan menaik, sesuai dengan kuncinya
- arsort()_ - mengurutkan array asosiatif dalam urutan menurun, sesuai dengan nilainya
- krsort() - urutkan array asosiatif dalam urutan menurun, sesuai dengan kuncinya
Urutkan Array dalam Urutan Naik - sort()
Contoh berikut mengurutkan elemen array $cars dalam urutan abjad menaik
Contoh berikut mengurutkan elemen array $numbers dalam urutan numerik menaik
Urutkan Array dalam Urutan Turun - rsort()
Contoh berikut mengurutkan elemen array $cars dalam urutan abjad menurun
Contoh berikut mengurutkan elemen array $numbers dalam urutan numerik menurun
Sortir Array (Ascending Order), Menurut Nilai - asort()
Contoh berikut mengurutkan array asosiatif dalam urutan menaik, menurut nilainya
Contoh
$umur = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($usia);
?>
Sortir Array (Urutan Naik), Menurut Kunci - ksort()
Contoh berikut mengurutkan array asosiatif dalam urutan menaik, menurut kuncinya
Contoh
$umur = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($umur);
?>
Sortir Array (Urutan Turun), Menurut Nilai - arsort()
Contoh berikut mengurutkan array asosiatif dalam urutan menurun, menurut nilainya
Contoh
$umur = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($usia);
?>
Sortir Array (Urutan Turun), Menurut Kunci - krsort()
Contoh berikut mengurutkan array asosiatif dalam urutan menurun, menurut kuncinya
Contoh
$umur = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($umur);
?>
Referensi Array PHP Lengkap
Untuk referensi lengkap semua fungsi array, buka Referensi Array PHP lengkap kami
Referensi berisi deskripsi singkat, dan contoh penggunaan, untuk setiap fungsi
Latihan PHP
Uji Diri Anda Dengan Latihan
Latihan
Gunakan metode larik yang benar untuk mengurutkan larik $colors menurut abjad
$colors = array("red", "green", "blue", "yellow"); ; _
Kirim Jawaban »