3. Sorting a array
Effect Ascending Descending User-defined
order
Sort array by
values, then
reassign indexes
starting with 0
sort() rsort() usort()
Sort array by
Values
asort() arsort() uarsort()
Sort array by
keys
ksort() krsort() ukrsort()
Monica Deshmane(H.V.Desai college,Pune) 3
4. Sorting a indexed array
1) sort()
•The sort() function sorts an array by the values.
•This function assigns new keys for the elements in
the array. Existing keys will be removed.
•This function returns TRUE on success, or FALSE
on failure.
$arr = array(10,30,20,15);
sort($arr );
print_r($arr );
//Array ( [0] => 10 [1] => 15 [2] => 20 [3] => 30 )
Monica Deshmane(H.V.Desai college,Pune) 4
6. Sorting a indexed array
2) rsort()
•The rsort() function sorts an array by the values in reverse
order.
•This function assigns new keys for the elements in the
array. Existing keys will be removed.
•This function returns TRUE on success, or FALSE on
failure.
$arr = array(10,30,20,15);
rsort($arr );
print_r($arr );
//Array ( [0] => 30 [1] => 20 [2] => 15 [3] => 10 )
Monica Deshmane(H.V.Desai college,Pune) 6
8. Sorting a indexed array
3) usort()
•The usort() function sorts an array by a user defined
comparison function.
•This function assigns new keys for the elements in
the array. Existing keys will be removed.
•This function returns TRUE on success, or FALSE
on failure.
Monica Deshmane(H.V.Desai college,Pune) 8
11. Sorting an associated array-with values
1)asort()
•The asort() function sorts an array by the values. The
values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
asort($book );
print_r ($book);
//Array ( [CPP] => Bjarne [PHP] => Orelly [C] =>
Richie [Java] => Sun )
Monica Deshmane(H.V.Desai college,Pune) 11
12. Sorting an associated array-with values
2)arsort()
•The asort() function sorts an array by the values in
reverse order. The values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
arsort($book );
print_r ($book);
//Array ([Java] => Sun [C] => Richie [PHP] =>
Orelly [CPP] => Bjarne)
Monica Deshmane(H.V.Desai college,Pune) 12
15. Sorting an associated array-with keys
1)ksort()
•The ksort() function sorts an array by the keys. The
values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
ksort($book );
print_r ($book);
//Array ( [C] => Richie [CPP] => Bjarne [Java] =>
Sun [PHP] => Orelly )
Monica Deshmane(H.V.Desai college,Pune) 15
16. Sorting an associated array-with keys
2)krsort()
•The ksort() function sorts an array by the keys in
reverse order. The values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
krsort($book );
print_r ($book);
//Array ([PHP] => Orelly [Java] => Sun [CPP] =>
Bjarne [C] => Richie)
Monica Deshmane(H.V.Desai college,Pune) 16
20. Natural order sorting with case insensitive manner
$output = natcasesort(input);
This function sorts an array by using a "natural order"
algorithm. The values keep their original keys.
This is case-insensitive.
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
echo "<br />";
natcasesort($temp_files);
echo "Natural order case insensitve: ";
print_r($temp_files);
Monica Deshmane(H.V.Desai college,Pune) 20
22. Sorting multiple array at once
array_multisort(array1,sorting order,[sorting
type,array2,array3...])
•array1 (required) Specifies an array
•sorting order (optional) Specifies the sorting order.
SORT_ASC Sort in ascending order (A-Z)
SORT_DESC Sort in descending order (Z-A)
•sorting type (optional) Specifies the type to use, when
comparing elements.
SORT_REGULAR Compare elements normally
SORT_NUMERIC Compare elements as numeric
values
SORT_STRING Compare elements as string
values
array2 (Optional) Specifies an array
array3 (Optional) Specifies an array
Monica Deshmane(H.V.Desai college,Pune) 22