0

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?

Rob
  • 573
  • 4
  • 13

2 Answers2

0
$m = array('A', 'G', 'Y', 'B');
asort($m);
print_r($m); //outpu: Array ( [0] => A [3] => B [1] => G [2] => Y ) 
Ghostff
  • 1,407
  • 3
  • 18
  • 30
  • Thanks Chrys and I'L'I, `asort()` got me half way there. I ended up using `array_shift()` and `array_pop()` to get the highest and lowest values. – Rob Jan 17 '16 at 04:47
0

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
Community
  • 1
  • 1
Rob
  • 573
  • 4
  • 13