U PHP-u postoji vise nacina za sortiranje podataka u nizu.
Funkciju sort() mozete koristiti kod nizova sa numerickim indeksima.
Primer: kreiranje niza, prikazivanje, sortiranje niza i ponovno prikazivanje niza.
<?php
$motor[0] = "suzuki";
$motor[1] = "ducati";
$motor[2] = "ktm";
print_r($motor);
sort($motor);
print_r($motor);
?>
Rezultat:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => ducati [1] => ktm [2] => suzuki )
Ako zelite da sortirate po obrnutom redosledu, koristite funkciju rsort().
Primer 2:
<?php
$motor[0] = "suzuki";
$motor[1] = "ducati";
$motor[2] = "ktm";
print_r($motor);
rsort($motor);
print_r($motor);
?>
Rezultat:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => suzuki [1] => ktm [2] => ducati )
Ako imate niz koji koristi kljuceve koristite funkciju asort(), koja zadrzava kljuceve, jer ako koristite funkcije sort() i rsort(), kljucevi ce biti zamenjeni brojevima.
Primer 3:
<?php
$motor["Japan"] = "suzuki";
$motor["Italija"] = "ducati";
$motor["Austrija"] = "ktm";
print_r($motor);
asort($motor);
print_r($motor);
?>
Rezultat:
Array ( [Japan] => suzuki [Italija] => ducati [Austrija] => ktm ) Array ( [Italija] => ducati [Austrija] => ktm [Japan] => suzuki )
Za ovakvo sortiranje samo po obrnutom redosledu mozete koristiti funkciju arsort(). Ako zelite da ovakav niz sortirate, na bazi kljuceva, a ne na bazi vrednosti, koristite funkciju ksort(). Ako zelite mozete da definisete svoje operacije sortiranja, tj. da napravite svoju funkciju, koristite funkciju usort().
In PHP there are several ways to sort data in array.
Function sort() can be used with arrays with numerical index.
Example: creating array, displaying array, sorting array and again displaying array.
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ducati";
$motorcycle[2] = "ktm";
print_r($motorcycle);
sort($motorcycle);
print_r($motorcycle);
?>
Result:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => ducati [1] => ktm [2] => suzuki )
If you want to sort in reverse order, use rsort().
Example 2:
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ducati";
$motorcycle[2] = "ktm";
print_r($motorcycle);
rsort($motorcycle);
print_r($motorcycle);
?>
Result:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => suzuki [1] => ktm [2] => ducati )
If you have array that uses the keys, use function asort(), which holds the keys, because if you use functions sort() and rsort(), the keys will be replaced by numbers.
Example 3:
<?php
$motorcycle["Japan"] = "suzuki";
$motorcycle["Italy"] = "ducati";
$motorcycle["Austria"] = "ktm";
print_r($motorcycle);
asort($motorcycle);
print_r($motorcycle);
?>
Result:
Array ( [Japan] => suzuki [Italy] => ducati [Austria] => ktm ) Array ( [Italy] => ducati [Austria] => ktm [Japan] => suzuki )
For this kind of sorting, only in the reverse order, you can use function arsort(). If you want this kind of array to sort, based on keys, and not on the basis of values, use function ksort(). If you want you can define your sorting operations, ie. to make its function, use function usort().
Funkciju sort() mozete koristiti kod nizova sa numerickim indeksima.
Primer: kreiranje niza, prikazivanje, sortiranje niza i ponovno prikazivanje niza.
<?php
$motor[0] = "suzuki";
$motor[1] = "ducati";
$motor[2] = "ktm";
print_r($motor);
sort($motor);
print_r($motor);
?>
Rezultat:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => ducati [1] => ktm [2] => suzuki )
Ako zelite da sortirate po obrnutom redosledu, koristite funkciju rsort().
Primer 2:
<?php
$motor[0] = "suzuki";
$motor[1] = "ducati";
$motor[2] = "ktm";
print_r($motor);
rsort($motor);
print_r($motor);
?>
Rezultat:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => suzuki [1] => ktm [2] => ducati )
Ako imate niz koji koristi kljuceve koristite funkciju asort(), koja zadrzava kljuceve, jer ako koristite funkcije sort() i rsort(), kljucevi ce biti zamenjeni brojevima.
Primer 3:
<?php
$motor["Japan"] = "suzuki";
$motor["Italija"] = "ducati";
$motor["Austrija"] = "ktm";
print_r($motor);
asort($motor);
print_r($motor);
?>
Rezultat:
Array ( [Japan] => suzuki [Italija] => ducati [Austrija] => ktm ) Array ( [Italija] => ducati [Austrija] => ktm [Japan] => suzuki )
Za ovakvo sortiranje samo po obrnutom redosledu mozete koristiti funkciju arsort(). Ako zelite da ovakav niz sortirate, na bazi kljuceva, a ne na bazi vrednosti, koristite funkciju ksort(). Ako zelite mozete da definisete svoje operacije sortiranja, tj. da napravite svoju funkciju, koristite funkciju usort().
In PHP there are several ways to sort data in array.
Function sort() can be used with arrays with numerical index.
Example: creating array, displaying array, sorting array and again displaying array.
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ducati";
$motorcycle[2] = "ktm";
print_r($motorcycle);
sort($motorcycle);
print_r($motorcycle);
?>
Result:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => ducati [1] => ktm [2] => suzuki )
If you want to sort in reverse order, use rsort().
Example 2:
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ducati";
$motorcycle[2] = "ktm";
print_r($motorcycle);
rsort($motorcycle);
print_r($motorcycle);
?>
Result:
Array ( [0] => suzuki [1] => ducati [2] => ktm ) Array ( [0] => suzuki [1] => ktm [2] => ducati )
If you have array that uses the keys, use function asort(), which holds the keys, because if you use functions sort() and rsort(), the keys will be replaced by numbers.
Example 3:
<?php
$motorcycle["Japan"] = "suzuki";
$motorcycle["Italy"] = "ducati";
$motorcycle["Austria"] = "ktm";
print_r($motorcycle);
asort($motorcycle);
print_r($motorcycle);
?>
Result:
Array ( [Japan] => suzuki [Italy] => ducati [Austria] => ktm ) Array ( [Italy] => ducati [Austria] => ktm [Japan] => suzuki )
For this kind of sorting, only in the reverse order, you can use function arsort(). If you want this kind of array to sort, based on keys, and not on the basis of values, use function ksort(). If you want you can define your sorting operations, ie. to make its function, use function usort().
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.