Tools, FAQ, Tutorials:
Sorting an Array by Values in PHP
How To Sort an Array by Values in PHP?
✍: FYIcenter.com
Sorting an array by values is doable by using the sort() function. It will re-order all pairs of keys and values based on the alphanumeric order of the values. Then it will replace all keys with integer keys sequentially starting with 0. So using sort() on arrays with integer keys (traditional index based array) is safe. It is un-safe to use sort() on arrays with string keys (maps). Be careful. Here is a PHP script on how to use sort():
<?php
$mixed = array();
$mixed["Zero"] = "PHP";
$mixed[1] = "Perl";
$mixed["Two"] = "Java";
$mixed["3"] = "C+";
$mixed[""] = "Basic";
$mixed[] = "Pascal";
$mixed[] = "FORTRAN";
sort($mixed);
print("Sorted by values:\n");
print_r($mixed);
?>
This script will print:
Sorted by values:
Array
(
[0] => Basic
[1] => C+
[2] => FORTRAN
[3] => Java
[4] => PHP
[5] => Pascal
[6] => Perl
)
⇒ Joining Keys and Values into an Array in PHP
⇐ Sorting an Array by Keys in PHP
2017-01-21, ∼2471🔥, 0💬
Popular Posts:
How To Merge Cells in a Column? If you want to merge multiple cells vertically in a row, you need to...
How To Read a File in Binary Mode in PHP? If you have a file that stores binary data, like an execut...
How To Access a Global Variable inside a Function? in PHP? By default, global variables are not acce...
Where to find EPUB Sample Files? Here is a list of EPUB sample files collected by FYIcenter.com team...
How To Avoid the Undefined Index Error in PHP? If you don't want your PHP page to give out errors as...