PHP sort: How to Sort an Array in PHP

0
30
PHP sort Function Example | PHP Sorting Arrays Tutorial

The sort() function is used to sort the array in ascending order, i.e., smaller to greater. The elements are reordered from lowest to highest when the sort() function is applied to an array.

PHP sort

PHP sort() is a built-in function that deals with sorting arrays, and this document exists to help sort it all out. The elements in the array can be sorted in alphabetical or numerical order, descending or ascending. The sort() function sorts an indexed array in ascending order.

The function provides us with the 6 sorting types, according to which an array can be sorted.

Syntax

See the following syntax of the sort() function in PHP.

sort(array, sorttype)

Parameters

An array argument is required, and it specifies the array to sort.

The sorttype is optional and specifies how to compare the array of elements/items. Possible values.

  1. SORT_REGULAR – When you pass 0 or SORT_REGULAR in the sorting_type parameter, the elements in the array are usually compared.
  2. SORT_NUMERIC – When you pass 1 or SORT_NUMERIC in the sorting_type parameter, the elements in an array are compared numerically.
  3. SORT_STRING – When you pass 2 or SORT_STRING in the sorting_type parameter, the elements in the array are compared string-wise.
  4. SORT_LOCALE_STRING – When you pass or SORT_LOCALE_STRING in the sorting_type parameter, the elements in an array are compared as strings based on the current locale.
  5. SORT_NATURAL – When you pass 4 or SORT_NATURAL in the sorting_type parameter, the elements in the array are compared as a string using natural ordering.
  6. SORT_FLAG_CASE – When you pass 5 or SORT_FLAG_CASE in the sorting_type parameter, the elements in the array are compared as strings. The elements are treated as case-insensitive and then compared. It can be used with | (bitwise operator) with the SORT_NATURAL or SORT_STRING.

How to Sort Array in PHP

To sort an array in PHP, use the sort() method. The sort() method sorts an actual array, and the changes are reflected in the original array itself.

See the following code.

<?php

$numbers = array(21, 11, 19, 29, 46);
echo 'Before Sort';
print_r($numbers);
sort($numbers);
echo 'After Sort';
print_r($numbers);

See the output.

➜  pro php app.php
Before SortArray
(
    [0] => 21
    [1] => 11
    [2] => 19
    [3] => 29
    [4] => 46
)
After SortArray
(
    [0] => 11
    [1] => 19
    [2] => 21
    [3] => 29
    [4] => 46
)
➜  pro

Use the sort() function to sort the string case-sensitively

We pass the second parameter SORT_STRING to do the sort string case-sensitively.

<?php

$arr = array("Millie", "Finn", "Gaten", "Caleb", "Noah", "Sadie"); 
  
sort($arr, SORT_STRING);  
  
print_r($arr);

See the output.

➜  pro php app.php
Array
(
    [0] => Caleb
    [1] => Finn
    [2] => Gaten
    [3] => Millie
    [4] => Noah
    [5] => Sadie
)
➜  pro

Use the sort() function to sort the string case-insensitively

To do the sort string case-sensitively, we pass the second parameter SORT_STRING | SORT_FLAG_CASE. See the following code.

<?php

$strangerThings = array("Millie", "finn", "Gaten", "caleb", "Noah", "sadie"); 
  
sort($strangerThings, SORT_STRING | SORT_FLAG_CASE);  
  
print_r($strangerThings);

See the output.

➜  pro php app.php
Array
(
    [0] => caleb
    [1] => finn
    [2] => Gaten
    [3] => Millie
    [4] => Noah
    [5] => sadie
)
➜  pro

That’s it for this tutorial.

Recommended Posts

PHP array_replace

PHP array_walk

PHP array_flip

PHP array_reduce

PHP array_count

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.