dotjoon — 2012-10-06T18:54:18-04:00 — #1
min(array(48, 9, 52, 183));
I can find the smallest value in an Array with the code above which is helped by guido2004 and logic_earth in the former post.
Now I like to find the 2nd smallest value which is 48 in the Array.
I guess I have 2 ways.
The 1st way is that finding the 2nd smallest value directly if there is any code or function in PHP.
The other way is that creates a new Array with the removing or deleting the smallest value which is 9 in the Array.
The new Array will be newArray[48, 52, 183) and the code below will find my target value 48.
min(array(48, 52, 183));
How can I find the 2nd smallest value directly(the 1st way)?
How can I get remove an array value which is 9 for finding the second smallest value(the other way)?
How can I get my target result?
logic_earth — 2012-10-06T19:21:50-04:00 — #2
sort( $array, SORT_NUMERIC );
$smallest = array_shift( $array );
$smallest_2nd = array_shift( $array );
dotjoon — 2012-10-06T20:20:41-04:00 — #3
Thank you very much, logic_earth.
cups — 2012-10-07T06:40:21-04:00 — #4
use sort() and access the 2nd array item.
$e = array($a, $b, $c, $d);
sort( $e );
echo $e; // 48