Tech

How to Naturally Sort Multidimensional Arrays with PHP

Author By Chandan Kumar
December 26, 2020
8 min read
Share:
How to Naturally Sort Multidimensional Arrays with PHP

The sorting process is known as “natural ordering” or “multidimensional array natural sorting.”

  1. Natural Sorting of Multidimensional Array

    Sorting arrays with more than one dimension can be challenging for beginners. PHP can compare two text strings or two numbers. Still, in a multidimensional array, each element is an array.
    How to Sort Multidimensional Arrays with PHP

  2. PHP Procedure to Sort Multidimensional Array Naturally

    PHP does not know how to compare two arrays, especially if you wish to sort the array in a human-readable format while simultaneously maintaining the key/value pairs. You can sort the multidimensional array with the “natural order” algorithm.Sorting Multidimensional Arrays with PHP

Here’s a Multidimensional Array:

$data = [
  [
    'id' => 1794,
    'size' => 1,
    'color' => 'Red',
  ],
  [
    'id' => 1826,
    'size' => '2XL',
    'color' => 'Red',
  ],
  [
    'id' => 1827,
    'size' => 3,
    'color' => '',
  ],
  [
    'id' => 1825,
    'size' => 30,
    'color' => 'Black'
  ],
  [
    'id' => 1828,
    'size' => '3XL',
    'color' => 'Blue'
  ],
  [
    'id' => 1829,
    'size' => 'Medium',
'color' => 'Blue'
  ],
  [
    'id' => 1795,
    'size' => 'XS',
    'color' => 'Red'
  ]
];

The above array can be sorted in natural order by using the following code:

usort($data, function($a, $b) {
    return strnatcasecmp($a['size'], $b['size']);
});

//Print output
echo "<pre>";
print_r($aa);

strnatcasecmp() function used in the above code compares two strings using a “natural” algorithm. It returns the result based on input arguments, where the first argument specifies the first string to compare, and the second argument specifies the second string to compare.

Executing the above code will produce the following output:

Array
(
    [0] => Array
        (
            [id] => 1794
            [size] => 1
            [color] => Red
        )

    [1] => Array
        (
            [id] => 1826
            [size] => 2XL
            [color] => Red
        )

    [2] => Array
        (
            [id] => 1827
            [size] => 3
            [color] => 
        )

    [3] => Array
        (
            [id] => 1828
            [size] => 3XL
            [color] => Blue
        )

    [4] => Array
        (
            [id] => 1825
            [size] => 30
            [color] => Black
        )

    [5] => Array
        (
            [id] => 1829
            [size] => Medium
            [color] => Blue
        )

    [6] => Array
        (
            [id] => 1795
            [size] => XS
            [color] => Red
        )
)
Chandan Kumar

Chandan Kumar

Related Articles

Continue reading with these hand-picked articles on similar topics.

Website Launch Checklist 2025: 20+ Ultimate Before and After Launch Tasks
Tech
Website Launch Checklist 2025: 20+ Ultimate Before and After Launch Tasks
Launching a website? Don’t miss a step! This all-in-one checklist covers everything from planning to promotion for a seamless, successful launch.
Chandan Kumar March 24, 2025
A Complete Guide to reCaptcha Accessibility Solution
Tech
A Complete Guide to reCaptcha Accessibility Solution
Recaptcha accessibility solutions come in various forms, including text-based CAPTCHAs, image-based CAPTCHAs, reCAPTCHAs, and mathematical CAPTCHAs.
Chandan Kumar July 7, 2022
Web Application Development Best Practices & Resources
Tech
Web Application Development Best Practices & Resources
Businesses today require custom web applications that provide an optimal user experience across devices, platforms, and browsers to increase their online presence.
Chandan Kumar June 21, 2021