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?

asked Jan 17, 2016 at 3:52
1
  • 1
    ...asort() = values ksort() = keys... Commented Jan 17, 2016 at 3:58

2 Answers 2

0
$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
1
  • 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. Commented Jan 17, 2016 at 4: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
answered Jan 17, 2016 at 4:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.