Home > Article > Backend Development > How to sort an array in php through the sort() function, sort array_PHP tutorial
The example in this article describes how PHP sorts an array through the sort() function. Share it with everyone for your reference. The specific analysis is as follows:
The sort() function is used to sort the array. This function assigns new key names to the cells in the array. The original key name will be deleted.
Returns TRUE if successful, FALSE otherwise.
$alpha = array ("x", "a", "f", "c"); sort( $alpha ); foreach ( $alpha as $var ) { print "$var<br />"; }
Example 2
<?php $my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse"); sort($my_array); print_r($my_array); ?>
Return results
Array ( [0] => Cat [1] => Dog [2] => Horse )
I hope this article will be helpful to everyone’s PHP programming design.