0

I am looking for a way to explicitly change some array keys as they will always be the same and the array length will always be the same and then output a single key based on the lowest value. Here is where I am at:

The array code itself look like this:

$t = array($low_score_1,$low_score_2,$low_score_3,$low_score_4,$low_score_5,$low_score_6,$low_score_7,$low_score_8);

Output example:

array(8) { [0]=> string(2) "11" [1]=> string(2) "15" [2]=> string(2) "13" [3]=> string(2) "12" [4]=> string(2) "18" [5]=> string(2) "16" [6]=> string(2) "16" [7]=> string(2) "14" }

So I want to change all 8 keys to each be a specific string. So I need to change the keys and then only output the key of the lowest value in the array. I can only at the moment output the value of the lowest in the array like so:

echo min($t);

And from the array above you can see that 11 is the lowest so that is the one I want to show BUT by ke and not value...

UPDATE

I have managed to set my keys and output both the keys with their retrospective pairs but I just want to show the lowest value by its key.

        $t = array(
        'a' => $low_score_1,
        'b' => $low_score_2,
        'c' => $low_score_3,
        'd' => $low_score_4,
        'e' => $low_score_5,
        'f' => $low_score_6,
        'g' => $low_score_7,
        'h' => $low_score_8,
        );

reset($t);
while (list($key, $val) = each($t)) {
    echo "$key => $val\n";
}

The output of this looks like:

a => 11 b => 15 c => 13 d => 12 e => 18 f => 16 g => 16 h => 14
3
  • And a downvote for what reason exactly? Its very clear in what my objective is AND I have made an attempt to get to my goal but I just need some advice... admin please explore why this has been downvoted... Commented Jan 29, 2017 at 10:16
  • Didn't downvote but It's a simple 'findMinimum' problem Commented Jan 29, 2017 at 10:17
  • @OfirBaruch how do you mean sorry? Im able to find the minimum no problem what I want to do is set the keys so 0 becomes score_1, 1 becomes score_2 and then output the kay instead of the value... Commented Jan 29, 2017 at 10:18

1 Answer 1

1

As mentioned it's a simple 'find minimum' problem. Only that you want to save the key of the minimum value.

        $t = array($low_score_1,$low_score_2,$low_score_3,$low_score_4,$low_score_5,$low_score_6,$low_score_7,$low_score_8);

  //Setting new keys        
$t2 = array();
    foreach($t as $key => $val){
     $key2 = 'score_' . ($key+1);
     $t2[$key2] = $val;
    }

//Finding the minimum
$min = $t2['score_1'];
$min_key = 0;

    foreach($t2 as $key => $val){
      if($val < $min){
        $min = $val;
        $min_key = $key;
     }
    }
        //output
        print_r($t2);
        echo $min; // the min value
        echo $min_key; // the key of the min value
Sign up to request clarification or add additional context in comments.

8 Comments

I want to change the keys to specific string names, then just output the lowest value from the array but by its key and not by value.
Just a sec, checking something
fixed the code. Test the new code. $min_key will hold the key for the minimum value's key.
I need to set all 8 keys manually, At the moment your code is just settings the key to 0. So there will only ever be 8 in the array so key 0 needs to become the string 'score_1' etc
I don't understand your comment. Please edit your question and elaborate about it. What is the full desired output that you're looking for?
|

Your Answer

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.