I have an array that is populated with 4 alphabetical letters from 'A' to 'Z'. I'm trying to find the lowest value where 'Z' is the lowest and 'A' the highest (like a grading system). For example:
$abc = ['A', 'G', 'Y', 'B'];
In this case Y is the lowest grade, but obviously min()
and max()
won't work since they give me the opposite of what I'm looking for. Is there a way to do it in PHP without writing my own function?
2 Answers 2
$m = array('A', 'G', 'Y', 'B');
asort($m);
print_r($m); //outpu: Array ( [0] => A [3] => B [1] => G [2] => Y )
answered Jan 17, 2016 at 4:03
-
Thanks Chrys and I'L'I,
asort()
got me half way there. I ended up usingarray_shift()
andarray_pop()
to get the highest and lowest values.Rob– Rob2016年01月17日 04:47:26 +00:00Commented Jan 17, 2016 at 4:47
Solution using array_shift()
and array_pop()
. Credit to: Jeremy Ruten
$abc = array('A', 'G', 'Y', 'B');
asort($abc);
print_r($abc); // Array ( [0] => A [3] => B [1] => G [2] => Y )
$highest = array_shift($abc);
$lowest = array_pop($abc);
echo "Highest: $highest. Lowest: $lowest"; // Highest: A. Lowest: Y
lang-php
asort()
= valuesksort()
= keys...