Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sorting the elements of array

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 950
    Comment on it

    In this tutorial we will learn how to sort the elements of an array on the basis of the keys or values as per the requirement.We will discuss the following sorting functions with example:

    1. sort()

    2. rsort()

    3. asort()

    4. ksort()

    5. arsort()

    6. krsort()


    1. sort() is used for sorting elements of an array in ascending order
    2. $fruits = array('mango','grapes','apple','pineapple');
      sort($fruits);
      
      $length= sizeof($fruits);
      
      for($i=0;$i<$length;$i++){
      echo $fruits[$i];echo '<br>';    
      
      }
      output:
      
      apple
      grapes
      mango
      pineapple
      

    3. rsort() is used for sorting elements of an array in descending order
    4. $fruits = array('mango','grapes','apple','pineapple');
      rsort($fruits);
      
      $length= sizeof($fruits);
      
      for($i=0;$i<$length;$i++){
      echo $fruits[$i];echo '<br>';    
      
      }
      
      output:
      
      pineapple
      mango
      grapes
      apple
      

    5. asort() is implemented on associate array that returns array elements sorted in ascending order with respect to their values, for each key
    6. $fruits = array('1'=>'mango','2'=>'grapes','3'=>'apple','4'=>'pineapple');
      asort($fruits);
      
      $length= sizeof($fruits);
      foreach($fruits as $key=> $key_value){
      echo "Key=".$key."; value=".$key_value;echo'<br>';    
      }
      
      output:
      
      Key=3; value=apple
      Key=2; value=grapes
      Key=1; value=mango
      Key=4; value=pineapple
      

    7. ksort is implemented on associate array that returns array element sorted in ascending order with respect to their keys
    8. $fruits = array('1'=>'mango','2'=>'grapes','3'=>'apple','4'=>'pineapple');
      ksort($fruits);
      
      $length= sizeof($fruits);
      foreach($fruits as $key=> $key_value){
      echo "Key=".$key."value=".$key_value;echo'<br>';    
      }
      
      output:
      
      Key=1value=mango
      Key=2value=grapes
      Key=3value=apple
      Key=4value=pineapple
      

    9. arsort is implemented on associate array that returns elements in descending order with respect to the values of each keys.
    10. $fruits = array('1'=>'mango','2'=>'grapes','3'=>'apple','4'=>'pineapple');
      arsort($fruits);
      
      foreach($fruits as $key=> $key_value){
      echo "Key=".$key."; value=".$key_value;echo'<br>';    
      }
      
      output:
      
      Key=4; value=pineapple
      Key=1; value=mango
      Key=2; value=grapes
      Key=3; value=apple
      

    11. krsort is implemented on associate array that returns elements in descending order with respect to their keys
    12. $fruits = array('1'=>'mango','2'=>'grapes','3'=>'apple','4'=>'pineapple');
      krsort($fruits);

      $length= sizeof($fruits);

      foreach($fruits as $key=> $key_value){ echo "Key=".$key."; value=".$key_value;echo'<br>' ;

      }

      output:

      Key=4; value=pineapple Key=3; value=apple Key=2; value=grapes Key=1; value=mango

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: