1

This is my array :

10908  :int 110
10280  :int 175
10278  :int 585
10277  :int 3015
10275  :int 2835

The index is the ID of the element. I want to know which ID has the bigest value.

When I sort it I lose the value of the Index. How can I sort the index in function of the value?

2

3 Answers 3

4

If you really need to sort it and want to keep the association between keys and values, use asort($array).

You can find a solution for your specific problem here: Return index of highest value in an array

Sign up to request clarification or add additional context in comments.

Comments

1

You need asort(), that sorts your array while keeping the keys.

And to get the highest key value, you can use max(array_keys($your_array)); regardless of the sort order of the array.

Comments

1

@CE_ use asort() like below example:

    <?php
     $arr = array(10908 => 110, 10280 => 175, 10278 => 585, 10277 => 3015, 10275 => 2835);
    print_r($arr); //before sort
    asort($arr);
    print_r($arr); // after sort

This asort() function sort array along with the index

3 Comments

I'd like to suggest that you remove the closing ?> php tag... It is generally considered bad style since it causes endless issues for nothing... It is typically not required, except if you want to terminate php interpretation in the middle of some file.
i know but i follow the syntax rules, i know ?> not required but its my good habbit
Actually it is not a good habit, since it causes issues without offering any benefit. And the "syntax rules" do not require you to close a php block in general...

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.