phparraysasort

PHP Showing only first result of array after sorting with asort?


I have an array containing only numbers, i want to sort them using asort so it shows the lowest number first.

but now i have the array sorted, how do i make it show only the first result?

For instance, this is my array:

Array ( [0] => 399 [1] => 349  ) 

and after asort:

Array ( [1] => 349 [0] => 399 ) 

how do i echo only the first result after i asort as i cant just use Array[1] because it might not always be [1] etc..

Sorry if this is a dumb question, but its late and my brain has ceased to function correctly lol.


Solution

  • Just use sort() which won't maintain key association.

    $array = array(399, 349);
    sort($array);
    print_r($array);
    // Array ( [0] => 349 [1] => 399 )
    

    Demo