AppDividend
Latest Code Tutorials

PHP sort Function Example | How To Sort Array In PHP

0

PHP sort() Function Example | PHP Sorting Arrays Tutorial is today’s topic. PHP has several functions that deal with sorting arrays, and this document exists to help sort it all out. The items in the array can be sorted in alphabetical or numerical order, descending or ascending. A sort() function sorts an indexed array in ascending order.

PHP sort() Function

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

It sorts an actual array, and the changes are reflected in an original array itself.

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

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

sort(array, sorttype)

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

The sorttype is optional and specifies how to compare the array 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 compared usually.
  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 string based on 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

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 sort() function to sort the string case-sensitively

Okay, to do the sort string case-sensitively, we pass the second parameter SORT_STRING.

<?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 sort() function to sort the string case-insensitively

Okay, 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

Finally, PHP sort() Function Example | PHP Sorting Arrays Tutorial is over.

Recommended Posts

PHP Array Replace Example

PHP array_walk Example

PHP Array Flip Example

PHP Array Reduce Example

PHP Array Count Example

Leave A Reply

Your email address will not be published.

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