natcasesortSORT an ARRAY using a case insensitive
natural order algorithm.
This function returns TRUE on success or FALSE on failure.
This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations as decribed in the natural order algorthm - very close to human understanding.
If two members compare as equal, their relative order in the sorted ARRAY is undefined.
<?php
bool natcasesort ( arr &$array )
where,
&$array = The input ARRAY
?>
&$array
The input ARRAY.
EXERCISE
<?php
$arr01n = [ 'BLUE', 'Blue', 'blue',
'Green', 'green', 'GREEN',
'wHiTe' ];
echo 'The given ARRAY:<br>';
print_r($arr01n);
echo '<br><br>';
if(natcasesort($arr01n) == true)
{
echo 'The SORTED ARRAY:<br>';
print_r($arr01n);
}
else
{
echo 'The array can not be sorted!';
}
?>
RESULT
The given ARRAY:
Array
(
[0] => BLUE
[1] => Blue
[2] => blue
[3] => Green
[4] => green
[5] => GREEN
[6] => wHiTe
)
The SORTED ARRAY:
Array
(
[0] => BLUE
[1] => Blue
[2] => blue
[3] => Green
[4] => green
[5] => GREEN
[6] => wHiTe
)
EXERCISE
<?php
$arr02n = [ '0' => 'ONE', 'T' => 'TWO',
't' => 'three', 'F' => 'FOUR' ];
echo 'The given ARRAY:<br>';
print_r($arr02n);
echo '<br><br>';
if(natcasesort($arr02n) == true)
{
echo 'The SORTED ARRAY:<br>';
print_r($arr02n);
}
else
{
echo 'The array can not be sorted!';
}
?>
RESULT
The given ARRAY:
Array
(
[0] => ONE
[T] => TWO
[t] => three
[F] => FOUR
)
The SORTED ARRAY:
Array
(
[F] => FOUR
[0] => ONE
[t] => three
[T] => TWO
)
EXERCISE
<?php
$arr03n = [ 1 => 'São Paulo',
2 => 'Santo André',
3 => 'São Caetano',
4 => 'Marília' ];
echo 'The given ARRAY:<br>';
print_r($arr03n);
echo '<br><br>';
if(natcasesort($arr03n) == true)
{
echo 'The SORTED ARRAY:<br>';
print_r($arr03n);
}
else
{
echo 'The array can not be sorted!';
}
?>
RESULT
The given ARRAY:
Array
(
[1] => São Paulo
[2] => Santo André
[3] => São Caetano
[4] => Marília
)
The SORTED ARRAY:
Array
(
[4] => Marília
[2] => Santo André
[3] => São Caetano
[1] => São Paulo
)
The result of this exercise can be different on your system.
EXERCISE
<?php
setlocale(LC_ALL, "pt_BR", "pt-BR");
// setlocale(LC_ALL, "en_US", "en-US");
$arr04n = [1 => 33.33, 2 => 2.456, 5 => M_PI];
echo 'The given ARRAY:<br><pre>';
print_r($arr04n);
echo '</pre><br><br>';
if(natcasesort($arr04n) == true)
{
echo 'The SORTED ARRAY:<br><pre>';
print_r($arr04n);
echo '</pre>';
}
else
{
echo 'The array can not be sorted!';
}
?>
RESULT
This function is no longer compatible
with LOCALE in PHP 8.0.XX,
however, it is still compatible in PHP 7.4.XX.
You should verify this by running this exercise in both versions, of course, if possible.
EXERCISE
<?php
$arr05n = [1, '1', 'two', 'TWO', null, false, true];
echo 'The given ARRAY:<br>';
print_r($arr05n);
echo '<br><br>';
if(natcasesort($arr05n) == true)
{
echo 'The SORTED ARRAY:<br>';
print_r($arr05n);
}
else
{
echo 'The array can not be sorted!';
}
?>
RESULT
The given ARRAY:
Array
(
[0] => 1
[1] => 1
[2] => two
[3] => TWO
[4] =>
[5] =>
[6] => 1
)
The SORTED ARRAY:
Array
(
[4] =>
[5] =>
[0] => 1
[1] => 1
[6] => 1
[2] => two
[3] => TWO
)
EXERCISE
<?php
/*
* Test basic functionality of natcasesort()
*/
$array = array ('A01', 'a1', 'b10', 'a01', 'b01');
echo "Before sorting:<br>";
var_dump($array);
echo "<br><br>After Sorting:<br>";
var_dump(natcasesort($array));
echo '<br><br>';
var_dump($array);
?>
EXERCISE
<?php
/*
* Check position of internal array pointer
* after calling natcasesort()
*/
$array_arg = array ('img13', 'img20', 'img2', 'img1');
echo "<br>Initial Position of Internal Pointer:<br>";
echo key($array_arg) . " => " . current ($array_arg);
echo "<br><br>Call natcasesort():<br>";
var_dump(natcasesort($array_arg));
echo '<br><br>';
var_dump($array_arg);
echo "<br><br>Position of Internal Pointer in Passed Array:<br>";
echo key($array_arg) . " => " . current ($array_arg) . "\n";
?>
EXERCISE
<?php
/*
* Pass arrays where the keys are
* different data types to test
* behaviour of natcasesort()
*/
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
hello world
EOT;
// arrays with keys as different
// data types to be passed as $array_arg
$inputs = array(
// int data
/*1*/ 'int' => array(
0 => 'zero',
1 => 'one',
12345 => 'positive',
-2345 => 'negative',
),
// float data
/*2*/ 'float' => array(
10.5 => 'positive',
-10.5 => 'negative',
.5 => 'half',
),
/*3*/ 'extreme floats' => array(
12.3456789000e6 => 'large',
12.3456789000E-10 => 'small',
),
// null data
/*4*/ 'null uppercase' => array(
NULL => 'null 1',
),
/*5*/ 'null lowercase' => array(
null => 'null 2',
),
// boolean data
/*6*/ 'bool lowercase' => array(
true => 'lowert',
false => 'lowerf',
),
/*7*/ 'bool uppercase' => array(
TRUE => 'uppert',
FALSE => 'upperf',
),
// empty data
/*8*/ 'empty double quotes' => array(
"" => 'emptyd',
),
/*9*/ 'empty single quotes' => array(
'' => 'emptys',
),
// string data
/*10*/ 'string' => array(
"stringd" => 'stringd',
'strings' => 'strings',
$heredoc => 'stringh',
),
// undefined data
/*11*/ 'undefined' => array(
@$undefined_var => 'undefined',
),
// unset data
/*12*/ 'unset' => array(
@$unset_var => 'unset',
),
// duplicate values
/*13*/ 'duplicate' => array(
'foo' => 'bar',
'baz' => 'bar',
'hello' => 'world'
),
);
// loop through each element of $inputs
// to check the behavior of natcasesort()
$iterator = 1;
foreach($inputs as $input) {
echo "<br><br>Iteration: $iterator<br>";
var_dump( natcasesort($input) );
echo '<br>';
var_dump($input);
$iterator++;
};
?>
EXERCISE
<?php
/*
* Pass arrays of numeric data to test
* how natcasesort re-orders the array
*/
/*
if (PHP_INT_SIZE != 4)
die("skip this test is for 32bit platform only");
*/
$inputs = array (
// negative/positive integers array
array(11, -11, 21, -21, 31, -31, 0, 41, -41),
// float value array
array(10.5, -10.5, 10.5e2, 10.6E-2, .5, .01, -.1),
// mixed value array
array(.0001, .0021, -.01, -1, 0,
.09, 2, -.9, 10.6E-2, -10.6E-2, 33),
// array values contains
// minimum and maximum ranges
array(2147483647, 2147483648,
-2147483647, -2147483648,
-0, 0, -2147483649)
);
$iterator = 1;
foreach ($inputs as $array_arg) {
echo "<br><br>Iteration: $iterator<br>";
var_dump(natcasesort($array_arg));
echo '<br>';
var_dump($array_arg);
}
?>
EXERCISE
<?php
/*
* Pass arrays of string data to see
* how natcasesort() re-orders the array
*/
$inputs = array (
// group of escape sequences
array(null, NULL, "\a", "\cx", "\e", "\f",
"\n", "\t", "\xhh", "\ddd", "\v"),
// array contains combination of capital/small letters
array("lemoN", "Orange", "banana", "apple",
"Test", "TTTT", "ttt", "ww",
"x", "X", "oraNGe", "BANANA")
);
foreach ($inputs as $array_arg) {
var_dump( natcasesort($array_arg) );
echo '<br><br>';
var_dump($array_arg);
echo '<br><br>';
}
?>
EXERCISE
<?php
/*
* Pass an array of different hex values to test
* how natcasesort() re-orders it
*/
$unsorted_hex_array = [0x1AB, 0xFFF, 0xF, 0xFF,
0x2AA, 0xBB, 0x1ab, 0xff, -0xFF, 0, -0x2aa];
var_dump( natcasesort($unsorted_hex_array) );
echo '<br>';
var_dump($unsorted_hex_array);
echo '<br>';
?>
EXERCISE
<?php
/*
* Pass an array of referenced variables
* to test how natcasesort() re-orders it
*/
$value1 = 100;
$value2 = 33;
$value3 = 555;
echo "Initial test:<br>";
$array = array( &$value1 , &$value2, &$value3);
var_dump( natcasesort($array) );
var_dump($array);
echo "<br><br>Change \$value1:<br>";
$value1 = -29;
var_dump( natcasesort($array) );
echo '<br>';
var_dump($array);
?>
EXERCISE
<?php
/*
* Pass natcasesort() an infinitely
* recursive array to test how it is re-ordered
*/
$array = array (1, 3.00, 'zero', '2');
$array[] = &$array;
var_dump($array);
echo '<br><br>';
var_dump(@natcasesort($array));
echo '<br><br>';
var_dump($array);
?>
EXERCISE
<?php
/*
* Pass an array of octal values to test
* how natcasesort() re-orders it
*/
$unsorted_oct_array = [ 01235, 0321, 0345,
066, 0772, 077, -066, -0345, 0 ];
var_dump( natcasesort($unsorted_oct_array) );
echo '<br><br>';
var_dump($unsorted_oct_array);
?>
EXERCISE
<?php
/*
* Pass an array containing sub-arrays,
* ints, floats, strings, boolean, null
* and escape characters to
* test how natcasesort() re-orders it
*/
$mixed_values = array (
array(),
array( array(33, -5, 6),
array(11),
array(22, -55),
array()
),
-4, "4", 4.00, "b", "5", -2, -2.0, -2.98989, "-.9", "True", "",
NULL, "ab", "abcd", 0.0, -0, "abcd\x00abcd\x00abcd", '', true, false
);
// suppress errors as is generating
// a lot of "array to string" notices
var_dump( @natcasesort($mixed_values) );
echo "<br><br>";
var_dump($mixed_values);
?>