PHP Programing language

array_ksort() and array_krsort() Function in PHP
Previous Home Next

array_ksort()

The krsort() function sorts an associative array in descending order, according to the key.

Syntax:
krsort(array,sortingtype);

In above syntax, "array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:

  • 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
  • 1 = SORT_NUMERIC - Compare items numerically
  • 2 = SORT_STRING - Compare items as strings
  • 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
  • 4 = SORT_NATURAL - Compare items as strings using natural ordering
  • 5 = SORT_FLAG_CASE -

 

Example:

<?php
$salary=array("John"=>"25000","Roy"=>"17000","Smith"=>"23000");
ksort($salary);
foreach($salary as $x=>$x_value)
    {
    echo "Emp_Name=" . $x . ", Salary=" . $x_value;
    echo "<br>";
    }
?>

Output:

Emp_Name=John, Salary=25000
Emp_Name=Roy, Salary=17000
Emp_Name=Smith, Salary=23000

array_krsort()

The pos() function returns the value of the current element in an array.

Syntax:
pos(array)

In above syntax, "array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:

  • 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
  • 1 = SORT_NUMERIC - Compare items numerically
  • 2 = SORT_STRING - Compare items as strings
  • 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
  • 4 = SORT_NATURAL - Compare items as strings using natural ordering
  • 5 = SORT_FLAG_CASE -

Example:

<?php
$salary=array("John"=>"25000","Roy"=>"17000","Smith"=>"23000");
krsort($salary);
foreach($salary as $x=>$x_value)
    {
    echo "Emp_Name=" . $x . ", Salary=" . $x_value;
    echo "<br>";
    }
?>

Output:

Emp_Name=Smith, Salary=23000
Emp_Name=Roy, Salary=17000
Emp_Name=John, Salary=25000

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: