Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- We can sort arrays according to index, alphabets etc. Here are some php functions to sort arrays in PHP
- ksort() and krsort() — For sorting associative arrays by key
- 1. sort() and rsort() — For sorting indexed arrays
- $colors = [30, 55, 1, 155];
- sort($colors);
- foreach ($colors as $color) {
- echo $color . '<br />';
- }
- 2. asort() and arsort() — For sorting associative arrays by value
- $array = ['Clark' => 30, 'Ram' => 45, 'Sita' => 31, 'Gita' => 25];
- asort($array);
- foreach($array as $key => $value) {
- echo $key . ' => ' . $value . '<br />';
- }
- */
- $array = ['Clark' => 30, 'Ram' => 45, 'Sita' => 31, 'Gita' => 25];
- krsort($array);
- foreach($array as $key => $value) {
- echo $key . ' => ' . $value . '<br />';
- }
Advertisement
Add Comment
Please, Sign In to add comment