I have this multidimensional array:
$data[] = array('name' => 'Mini 16', 'id' => 105);
$data[] = array('name' => 'Mini 15', 'id' => 5650);
$data[] = array('name' => 'Mini 100', 'id' => 9889);
$data[] = array('name' => 'Mini 20', 'id' => 587);
I want to order the array by name column sorting naturally, but is difficult for me.
The expected result:
[
['name' => 'Mini 15', 'id' => 5650],
['name' => 'Mini 16', 'id' => 105],
['name' => 'Mini 20', 'id' => 587],
['name' => 'Mini 100', 'id' => 9889]
]
You can use
usort()to sort the array by a custom function, and usestrnatcmp()to do the natural comparison of two strings like so:So before, your array was this:
And now it looks like:
Note that for lower versions of PHP, you won’t be able to use an anonymous function, and would instead need something like this: