Tech

How to Naturally Sort Multidimensional Arrays with PHP

Chandan Kumar
By Chandan Kumar
December 26, 2020
2 min read
Share The Blog:
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

Chandan Kumar doesn't just write code; he builds digital legacies. As the Founder and Team Lead at AvyaTech, Chandan combines high-level strategy with granular technical expertise to turn "what if" into "it's live." When he’s not steering his team through complex development sprints, he’s busy architecting the future of scalable, user-first technology.

Related Articles

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

MCP Servers for Real Estate Companies
Tech
MCP Servers for Real Estate Companies
Your real estate data already knows where the money is. MCP lets you ask it. One of our clients runs a real estate marketplace in the US. Buyers and sellers find agents through the platform, and the marketplace earns a fee when a deal closes. Some agents were closing deals with marketplace leads and never […]
Chandan Kumar November 11, 2026
Dedicated Development Team Model in 2026: Is It Right for Your Project?
Tech
Dedicated Development Team Model in 2026: Is It Right for Your Project?
You’ve scoped your project, you have a budget, and now you’re looking at three engagement options: Fixed Price, Time and Materials, and Dedicated Team. The first two feel familiar. The third raises questions. What exactly do you get? Who manages the work? How does it compare on cost and control? This article breaks down the […]
Chandan Kumar July 30, 2026
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