<?php
bool sort ( arr &$array [, int $sort_flags = SORT_REGULAR ] )
where,
&$array = The input ARRAY
$sort_flags = To control de sorting behavior
( SEE the below TABLE )
?>
CONSTANT | VALUE | OBSERVATIONS | DEFAULT |
SORT_REGULAR | 0 | compare items normally (don't change types). |
SORT_REGULAR |
SORT_NUMERIC | 1 | compare items numerically | |
SORT_STRING | 2 | compare items as strings | |
SORT_LOCALE_STRING | 5 | compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale. |
|
SORT_NATURAL | 6 | compare items as strings using "natural ordering" like natsort. | |
SORT_FLAG_CASE | 8 | can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively | |
ed48 |
<?php
$arr01s = ["x", "Z", "a", "y", "B", "k"];
echo '<pre>';
var_dump($arr01s);
echo '</pre>';
if(sort($arr01s) == true)
{
echo '<pre>';
var_dump($arr01s);
echo '</pre>';
}
?>
<?php
$arr02s = ["x", "Z", "a", "y", "B", "k", 5, 7, "9", "11"];
echo '<pre>';
var_dump($arr02s);
echo '</pre>';
if(sort($arr02s, SORT_REGULAR) == true)
{
echo '<pre>';
var_dump($arr02s);
echo '</pre>';
}
?>
<?php
$arr03s = ["R" => 'red', "G" => 'green', "B" => 'blue',
"H" => 'HUE', "S" => 'SATURATION', "K" => 'BLACK'];
echo '<pre>';
var_dump($arr03s);
echo '</pre>';
if(sort($arr03s, SORT_STRING) == true)
{
echo '<pre>';
var_dump($arr03s);
echo '</pre>';
}
?>
<?php
$arr04s = [ 1 => 100, 2 => 73, 'THREE' => 120,
'4' => 88, 'five' => '77', 6 => '120',
'8' => 'EIGHT', 9 => 'nine' ];
echo '<pre>';
var_dump($arr04s);
echo '</pre>';
if(sort($arr04s, SORT_NUMERIC) == true)
{
echo '<pre>';
var_dump($arr04s);
echo '</pre>';
}
?>
<?php
$arr05a = [1 => 33.33, 2 => 2.456, 5 => M_PI];
var_dump($arr05a);
echo '<br>';
setlocale(LC_ALL, "en_US", "en-US", "american",
"american english", "american-english",
"english-american", "english-us",
"english-usa", "enu", "us", "usa");
if(sort($arr05a, SORT_LOCALE_STRING) == true)
{
var_dump($arr05a);
}
echo '<br><br><br>ATTENTION...<br><br>This function is no longer compatible<br>
with LOCALE in PHP 8.0.XX,<br>
however, it is still compatible in PHP 7.4.XX.<br><br>';
$arr05b = [1 => 33.33, 2 => 2.456, 5 => M_PI];
var_dump($arr05b);
echo '<br>';
setlocale(LC_ALL, "pt_BR.utf-8", "pt-BR");
if(sort($arr05b, SORT_LOCALE_STRING) == true)
{
var_dump($arr05b);
}
?>
<?php
/*
* Testing sort() by providing
* arrays with default keys and assoc arrays
* to check the basic functionality
* with following flag values.
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_NUMERIC - compare items numerically
* SORT_STRING - compare items as strings
*/
echo "Basic functionality:<br>";
// associative array
// containing unsorted string values
$unsorted_strings = array(
"l" => "lemon", "o" => "orange",
"O" => "Orange", "O1" => "Orange1",
"o2" => "orange2", "O3" => "Orange3",
"o20" => "orange20", "b" => "banana",
);
// array with default keys
// containing unsorted numeric values
$unsorted_numerics = array( 100, 33, 555, 22 );
echo "<br>(string array)<br>'flag' value is default:<br>";
$temp_array = $unsorted_strings;
var_dump( sort($temp_array) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(numeric array)<br>'flag' value is default:<br>";
$temp_array = $unsorted_numerics;
var_dump( sort($temp_array) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(string array)<br>'flag' = SORT_REGULAR:<br>";
$temp_array = $unsorted_strings;
var_dump( sort($temp_array, SORT_REGULAR) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(numeric array)<br>'flag' = SORT_REGULAR:<br>";
$temp_array = $unsorted_numerics;
var_dump( sort($temp_array, SORT_REGULAR) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(string array)<br>'flag' = SORT_STRING:<br>";
$temp_array = $unsorted_strings;
var_dump( sort($temp_array, SORT_STRING) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(case insensitive)<br>'flag' = SORT_STRING|SORT_FLAG_CASE:<br>";
$temp_array = $unsorted_strings;
var_dump( sort($temp_array, SORT_STRING|SORT_FLAG_CASE) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(string array, natural)<br>'flag' = SORT_NATURAL:<br>";
$temp_array = $unsorted_strings;
var_dump( sort($temp_array, SORT_NATURAL) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(natural, case insensitive)<br>'flag' = SORT_NATURAL|SORT_FLAG_CASE:<br>";
$temp_array = $unsorted_strings;
var_dump( sort($temp_array, SORT_NATURAL|SORT_FLAG_CASE) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(numeric array)<br>'flag' = SORT_NUMERIC:<br>";
$temp_array = $unsorted_numerics;
var_dump( sort($temp_array, SORT_NUMERIC) );
// expecting : bool(true)
echo '<br>';
var_dump( $temp_array);
?>
<?php
/*
* Testing sort() by providing
* different octal array for $array argument
* with following flag values
* 1.flag value as default
* 2.SORT_REGULAR - compare items normally
* 3.SORT_NUMERIC - compare items numerically
*/
echo "Usage variations:<br><br>";
// an array containing unsorted octal values
$unsorted_oct_array = [01235, 0321, 0345,
066, 0772, 077, -066, -0345, 0];
echo "(octal value array)<br>'flag' value is default:<br>";
$temp_array = $unsorted_oct_array;
var_dump(sort($temp_array) );
// expecting : bool(true)
var_dump($temp_array);
echo "<br><br>(octal value array)<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $unsorted_oct_array;
var_dump(sort($temp_array, SORT_REGULAR) );
// expecting : bool(true)
var_dump($temp_array);
echo "<br><br>(octal value array)<br>'flag' value is SORT_NUMERIC:<br>";
$temp_array = $unsorted_oct_array;
var_dump(sort($temp_array, SORT_NUMERIC) );
// expecting : bool(true)
var_dump($temp_array);
?>
<?php
/*
* Testing sort() by providing
* mixed value array for $array argument
* with following flag values.
* flag value as default
* SORT_REGULAR - compare items normally
*/
echo "Usage variations:";
// mixed value array
$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
);
echo "<br><br>(mixed value array)<br>'flag' value is default:<br>";
$temp_array = $mixed_values;
var_dump(sort($temp_array) );
echo '<br><br>';
var_dump($temp_array);
echo "<br><br>(mixed value array)<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $mixed_values;
var_dump(sort($temp_array, SORT_REGULAR) );
echo '<br><br>';
var_dump($temp_array);
?>
<?php
/*
* Testing sort() by providing
* different integer/float value arrays
* for $array argument
* with following flag values
* 1. flag value as default
* 2. SORT_REGULAR - compare items normally
* 3. SORT_NUMERIC - compare items numerically
* 4. SORT_STRING - compare items as strings
*/
// group of various arrays
$various_arrays = array (
// negative/posative 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)
);
// set of possible flag values
$flag_value = array("SORT_REGULAR" => SORT_REGULAR,
"SORT_NUMERIC" => SORT_NUMERIC);
$count = 1;
echo "Various integer/float arrays:";
// loop through to test sort() with different arrays
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
echo "<br>With Default sort flag<br>";
$temp_array = $array;
var_dump(sort($temp_array) );
var_dump($temp_array);
// loop through $flag_value array
// and setting all possible flag values
foreach($flag_value as $key => $flag){
echo "<br><br>Sort flag = $key<br>";
$temp_array = $array;
var_dump(sort($temp_array, $flag) );
var_dump($temp_array);
}
$count++;
}
?>
<?php
/*
* Testing sort() by providing reference
* variable array with following flag values
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_NUMERIC - compare items numerically
*/
$value1 = 100;
$value2 = 33;
$value3 = 555;
// an array containing integer references
$unsorted_numerics = array( &$value1 , &$value2, &$value3);
echo "<br>(Reference variable array)<br>'flag' value is default:<br>";
$temp_array = $unsorted_numerics;
var_dump( sort($temp_array) );
// expecting : bool(true)
var_dump( $temp_array);
echo "<br><br>(Reference variable array)<br>'flag' = SORT_REGULAR:<br>";
$temp_array = &$unsorted_numerics;
var_dump( sort($temp_array, SORT_REGULAR) );
// expecting : bool(true)
var_dump( $temp_array);
echo "<br><br>(Reference variable array)<br>'flag' = SORT_NUMERIC:<br>";
$temp_array = &$unsorted_numerics;
var_dump( sort($temp_array, SORT_NUMERIC) );
// expecting : bool(true)
var_dump( $temp_array);
?>
<?php
/*
* Testing sort() by providing
* different string arrays for $array argument
* with following flag values
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_STRING - compare items as strings
*/
$various_arrays = array (
// group of escape sequences
array(null, NULL, "\a", "\cx", "\e", "\f",
"\n", "\r", "\t", "\xhh", "\ddd", "\v"),
// array contains combination
// of capital/small letters
array("lemoN", "Orange", "banana", "apple",
"Test", "TTTT", "ttt", "ww", "x",
"X", "oraNGe", "BANANA")
);
$flags = array("SORT_REGULAR" => SORT_REGULAR,
"SORT_STRING" => SORT_STRING);
$count = 1;
echo "Various string arrays:";
// loop through to test sort() with different arrays
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
echo "<br>With Default sort flag:<br>";
$temp_array = $array;
var_dump(sort($temp_array) );
// expecting : bool(true)
var_dump($temp_array);
// loop through $flags array and
// setting all possible flag values
foreach($flags as $key => $flag){
echo "<br><br>Sort flag = $key<br>";
$temp_array = $array;
var_dump(sort($temp_array, $flag) );
// expecting : bool(true)
var_dump($temp_array);
}
$count++;
}
?>
<?php
/*
* Testing sort() by providing different
* hexa-decimal array for $array argument
* with following flag values
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_NUMERIC - compare items numerically
*/
// an array contains unsorted
// hexadecimal values
$unsorted_hex_array = [0x1AB, 0xFFF, 0xF, 0xFF,
0x2AA, 0xBB, 0x1ab,
0xff, -0xFF, 0, -0x2aa];
echo "(hexadecimal value array)
<br>'flag' value is default:<br>";
$temp_array = $unsorted_hex_array;
var_dump(sort($temp_array) );
// expecting : bool(true)
var_dump($temp_array);
echo "<br><br>(hexadecimal value array)
<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $unsorted_hex_array;
var_dump(sort($temp_array, SORT_REGULAR) );
echo '<br>';
// expecting : bool(true)
var_dump($temp_array);
echo "<br><br>(hexadecimal value array)
<br>'flag' value is SORT_NUMERIC:<br>";
$temp_array = $unsorted_hex_array;
var_dump(sort($temp_array, SORT_NUMERIC) );
echo '<br>';
// expecting : bool(true)
var_dump($temp_array);
?>
<?php
/*
* Testing sort() by providing bool value array
* for $array argument with following flag values.
* flag value as default
* SORT_REGULAR - compare items normally
*/
// bool value array
$bool_values = [true, false, TRUE, FALSE];
echo "(bool value array)
<br>'flag' value is default:<br>";
$temp_array = $bool_values;
var_dump(sort($temp_array) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(bool value array)
<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $bool_values;
var_dump(sort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(bool value array)
<br>'flag' value is SORT_NUMERIC:<br>";
$temp_array = $bool_values;
var_dump(sort($temp_array, SORT_NUMERIC) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(bool value array)
<br>'flag' value is SORT_STRING:<br>";
$temp_array = $bool_values;
var_dump(sort($temp_array, SORT_STRING) );
echo '<br>';
var_dump($temp_array);
?>
<?php
/*
* Testing sort() by providing arrays contains
* sub arrays for $array argument
* with flowing flag values
* flag value as default
* SORT_REGULAR - compare items normally
*/
// array of arrays
$various_arrays = array (
// null array
array(),
// array contains null sub array
array( array() ),
// array of arrays along with some values
array(44, 11, array(64, 61) ),
// array containing sub arrays
array(array(33, -5, 6), array(11), array(22, -55), array() )
);
$count = 1;
// loop through to test sort() with different arrays
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
// testing sort() function by supplying
// different arrays, flag value is default
echo "<br>With Default sort flag:<br>";
$temp_array = $array;
var_dump(sort($temp_array) );
echo '<br>';
var_dump($temp_array);
// testing sort() function by supplying
// different arrays, flag value = SORT_REGULAR
echo "<br><br>Sort flag = SORT_REGULAR<br>";
$temp_array = $array;
var_dump(sort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
$count++;
}
?>
<?php
/*
* Testing sort() by providing arrays
* with key values for $array argument
* with following flag values.
* 1.flag value as default
* 2.SORT_REGULAR - compare items normally
* To test the new keys for the
* elements in the sorted array.
*/
// list of arrays with key and values
$various_arrays = array(
array(5 => 55, 6 => 66, 2 => 22, 3 => 33, 1 => 11),
array ("fruits" => array("a" => "orange",
"b" => "banana",
"c" => "apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
),
array(1, 1, 8 => 1, 4 => 1, 19, 3 => 13),
array('bar' => 'baz', "foo" => 1),
array('a' => 1,'b' => array('e' => 2,
'f' => 3),'c' => array('g' => 4),'d' => 5),
);
$count = 1;
echo "Various arrays with key values:";
// loop through to test sort() with different arrays,
// to test the new keys for the elements in the sorted array
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
echo "<br>With Default sort flag<br>";
$temp_array = $array;
var_dump(sort($temp_array) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>Sort flag = SORT_REGULAR<br>";
$temp_array = $array;
var_dump(sort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
$count++;
}
?>