Bila anda sering membuat script PHP, kadang akan menemui data dalam bentuk array, misalkanya ingin mengambil data kurs dollar tabel HTML. Sebagai contoh kita akan mengolah array
// nama rilis Debian $data = ["Buzz" ,"Rex" ,"Bo" ,"Hamm" ,"Slink" ,"Potato" ,"Woody" ,"Sarge" ,"Etch" ,"Lenny" ,"Squeeze" ,"Wheezy" ,"Jessie" ,"Stretch" ,"Buster" ,"Bullseye"];
Menggunakan asort
asort : mengurutkan array berdasarkan value A-Z. Contoh
<?php // array.php // nama rilis Debian $data = ["Buzz" ,"Rex" ,"Bo" ,"Hamm" ,"Slink" ,"Potato" ,"Woody" ,"Sarge" ,"Etch" ,"Lenny" ,"Squeeze" ,"Wheezy" ,"Jessie" ,"Stretch" ,"Buster" ,"Bullseye"]; asort($data); print_r($data);
hasilnya
> php array.php Array ( [2] => Bo [15] => Bullseye [14] => Buster [0] => Buzz [8] => Etch [3] => Hamm [12] => Jessie [9] => Lenny [5] => Potato [1] => Rex [7] => Sarge [4] => Slink [10] => Squeeze [13] => Stretch [11] => Wheezy [6] => Woody )
Menggunakan arsort
arsort : mengurutkan array berdasarkan value Z-A. Contoh
<?php // array.php // nama rilis Debian $data = ["Buzz" ,"Rex" ,"Bo" ,"Hamm" ,"Slink" ,"Potato" ,"Woody" ,"Sarge" ,"Etch" ,"Lenny" ,"Squeeze" ,"Wheezy" ,"Jessie" ,"Stretch" ,"Buster" ,"Bullseye"]; arsort($data); print_r($data);
hasilnya
> php array.php Array ( [6] => Woody [11] => Wheezy [13] => Stretch [10] => Squeeze [4] => Slink [7] => Sarge [1] => Rex [5] => Potato [9] => Lenny [12] => Jessie [3] => Hamm [8] => Etch [0] => Buzz [14] => Buster [15] => Bullseye [2] => Bo )
Menggunakan sort
sort : mengurutkan array berdasarkan value A-Z, dan mereset urutan keynya. Contoh
<?php // array.php // nama rilis Debian $data = ["Buzz" ,"Rex" ,"Bo" ,"Hamm" ,"Slink" ,"Potato" ,"Woody" ,"Sarge" ,"Etch" ,"Lenny" ,"Squeeze" ,"Wheezy" ,"Jessie" ,"Stretch" ,"Buster" ,"Bullseye"]; sort($data); print_r($data);
hasilnya
> php array.php Array ( [0] => Bo [1] => Bullseye [2] => Buster [3] => Buzz [4] => Etch [5] => Hamm [6] => Jessie [7] => Lenny [8] => Potato [9] => Rex [10] => Sarge [11] => Slink [12] => Squeeze [13] => Stretch [14] => Wheezy [15] => Woody )
Menggunakan rsort
rsort : mengurutkan array berdasarkan value Z-A, dan mereset urutan keynya. Contoh
<?php // array.php // nama rilis Debian $data = ["Buzz" ,"Rex" ,"Bo" ,"Hamm" ,"Slink" ,"Potato" ,"Woody" ,"Sarge" ,"Etch" ,"Lenny" ,"Squeeze" ,"Wheezy" ,"Jessie" ,"Stretch" ,"Buster" ,"Bullseye"]; rsort($data); print_r($data);
hasilnya
> php array.php Array ( [0] => Woody [1] => Wheezy [2] => Stretch [3] => Squeeze [4] => Slink [5] => Sarge [6] => Rex [7] => Potato [8] => Lenny [9] => Jessie [10] => Hamm [11] => Etch [12] => Buzz [13] => Buster [14] => Bullseye [15] => Bo )