humanware

php_training_day6_array_sorting

Aug 7th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. <?php
  2.     /*
  3.     We can sort arrays according to index, alphabets etc. Here are some php functions to sort arrays in PHP
  4.     ksort() and krsort() — For sorting associative arrays by key
  5.  
  6.     1. sort() and rsort() — For sorting indexed arrays
  7.     $colors = [30, 55, 1, 155];
  8.     sort($colors);
  9.     foreach ($colors as $color) {
  10.         echo $color . '<br />';
  11.     }
  12.  
  13.     2. asort() and arsort() — For sorting associative arrays by value
  14.     $array = ['Clark' => 30, 'Ram' => 45, 'Sita' => 31, 'Gita' => 25];
  15.     asort($array);
  16.  
  17.     foreach($array as $key => $value) {
  18.         echo $key . ' => ' . $value . '<br />';
  19.     }
  20.     */
  21.  
  22.     $array = ['Clark' => 30, 'Ram' => 45, 'Sita' => 31, 'Gita' => 25];
  23.     krsort($array);
  24.  
  25.     foreach($array as $key => $value) {
  26.         echo $key . ' => ' . $value . '<br />';
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment