The natcasesort Function
The natcasesort
function sorts an array
as a human would.
This function preserves the associations
between keys and values.
Unlike natsort
,
it ignores character case.
Syntax
sort(array &$array, int $flags = SORT_REGULAR): bool
Example
Let's first perform sorting
using the natsort
function
with case sensitivity:
<?php
$arr = [
'Img12.png',
'Img10.png',
'img2.png',
'Img1.png'
];
natsort($arr);
var_dump($arr);
?>
Code execution result:
[
'Img10.png',
'img1.png',
'img2.png',
'img12.png',
]
Example
Now let's perform natural
sorting using the natcasesort
function:
<?php
$arr = [
'img12.png',
'Img10.png',
'img2.png',
'img1.png'
];
natcasesort($arr);
var_dump($arr);
?>
Code execution result:
[
'img1.png',
'img2.png',
'Img10.png',
'img12.png',
]
See Also
-
the
sort
function,
which sorts by element values in ascending order -
the
rsort
function,
which sorts by element values in descending order -
the
ksort
function,
which sorts by keys in ascending order -
the
krsort
function,
which sorts by keys in descending order -
the
asort
function,
which sorts by element values in ascending order while preserving keys -
the
arsort
function,
which sorts by element values in descending order while preserving keys -
the
natsort
function,
which sorts in a natural manner -
the
usort
function,
which sorts using a callback function -
the
uksort
function,
which sorts by keys using a callback function -
the
uasort
function,
which sorts using a callback function while preserving keys -
the
array_multisort
function,
which sorts multiple arrays