php array sort

PHP Array Sort by Value and Key

PHP array sort is an action to sort array elements in alphabetical or numerical order, descending or ascending in PHP. Some sort based on the array keys, whereas others by the values. in this tutorial, we are going to discuss how to sort PHP arrays by value and key in ascending and descending order.

PHP array sort content

Array sort functions

You can easily sort an array by the sort functions. Returns true if the sorting is successful, otherwise false. By default, most of these functions sort the array in ascending order.

The original function is sort() function. Along with the sort() function are other functions that do the same sort as the function but with a bit more control.

  • a: Sorting with key retention
  • k: Sort by key
  • r: Descending sort
  • u: Sort by user function

Here are the functions

  • sort(): sort array by value in ascending order
  • asort(): sort array by value in ascending order
  • arsort(): sort array by value in descending order
  • ksort(): sort array by key in ascending order
  • krsort(): sort array by key in descending order
  • rsort(): sort array by value in descending order
  • uasort(): sort array by user-defined function in ascending order
  • usort(): sort array by user-defined function in ascending order

Sort array in ascending order based on the value

PHP array sort by value can be done by sort() and asort() function in ascending order.

sort() function

$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");

sort($cars);

var_dump($cars);

Output

array (size=4)
  0 => string 'BMW' (length=3)
  1 => string 'Benz' (length=4)
  2 => string 'Ferrari' (length=7)
  3 => string 'Volvo' (length=5)

asort() function

The difference between this function and the sort() function is in the returned array keys. In the sort() function the returned keys are indexed and in the asort() function is the keys itself.

$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");

asort($cars);

var_dump($cars);

Output

array (size=4)
  'bmw' => string 'BMW' (length=3)
  'ben' => string 'Benz' (length=4)
  'fer' => string 'Ferrari' (length=7)
  'vol' => string 'Volvo' (length=5)

Sort array in descending order based on the value

PHP array sort by value can be done by rsort() and arsort() function in descending order.

rsort() function

$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");

rsort($cars);

var_dump($cars);

Output

array (size=4)
  0 => string 'Volvo' (length=5)
  1 => string 'Ferrari' (length=7)
  2 => string 'Benz' (length=4)
  3 => string 'BMW' (length=3)

arsort() function

The difference between this function and the rsort() function is in the returned array keys. In the rsort() function the returned keys are indexed and in the arsort() function is the keys itself.

$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");

arsort($cars);

var_dump($cars);

Output

array (size=4)
  'vol' => string 'Volvo' (length=5)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

Sort array in ascending order based on the key

PHP array sort by the key can be done by ksort() function in ascending order.

ksort() function

$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");

ksort($cars);

var_dump($cars);

Output

array (size=4)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'fer' => string 'Ferrari' (length=7)
  'vol' => string 'Volvo' (length=5)

Sort array in descending order based on the key

PHP array sort by the key can be done by krsort() function in descending order.

krsort() function

$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");

krsort($cars);

var_dump($cars);

Output

array (size=4)
  'vol' => string 'Volvo' (length=5)
  'fer' => string 'Ferrari' (length=7)
  'bmw' => string 'BMW' (length=3)
  'ben' => string 'Benz' (length=4)

The user-defined array sort function

usort() function

function hs_custom_sort($value1, $value2)
{
    return strcmp($value1, $value2);
}
$cars = array(
    "fer" => "Ferrari",
    "ben" => "Benz",
    "bmw" => "BMW",
    "vol" => "Volvo"
);

usort($cars, 'hs_custom_sort');

var_dump($cars);

Output

array (size=4)
  0 => string 'BMW' (length=3)
  1 => string 'Benz' (length=4)
  2 => string 'Ferrari' (length=7)
  3 => string 'Volvo' (length=5)

uasort() function

The difference between this function and the usort() function is in the returned array keys. In the usort() function the returned keys are indexed and in the uasort() function is the keys itself.

function hs_custom_sort($value1, $value2)
{
    return strcmp($value1, $value2);
}
$cars = array(
    "fer" => "Ferrari",
    "ben" => "Benz",
    "bmw" => "BMW",
    "vol" => "Volvo"
);

uasort($cars, 'hs_custom_sort');

var_dump($cars);

Output

array (size=4)
  'bmw' => string 'BMW' (length=3)
  'ben' => string 'Benz' (length=4)
  'fer' => string 'Ferrari' (length=7)
  'vol' => string 'Volvo' (length=5)

Array sort based on the local language

If your data contains characters other than English, PHP has a way to sort them. You can use PHP local for this.

setlocale(LC_ALL, 'sk_SK.utf8');

$words = ["ďateľ", "auto", "železo", "byt", "kocka", "dáma", "zem", "autor", "ceduľa", "čižma"];

sort($words, SORT_LOCALE_STRING);

var_dump($words);

Output

array (size=10)
  0 => string 'auto' (length=4)
  1 => string 'autor' (length=5)
  2 => string 'byt' (length=3)
  3 => string 'ceduľa' (length=7)
  4 => string 'dáma' (length=5)
  5 => string 'kocka' (length=5)
  6 => string 'zem' (length=3)
  7 => string 'čižma' (length=7)
  8 => string 'ďateľ' (length=7)
  9 => string 'železo' (length=7)

First of all, we set the desired local language and then sort. In this example, the Slovak language is defined.

More

Shopping Cart