0

I have an array as follows

Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[33636] => 1
[67376] => 2
[73250] => 1
[125313] => 2
[142062] => 1
[342520] => 2
[357301] => 2
[357303] => 1
[404419] => 1
[408957] => 1
[415891] => 2
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)

My aim is to order them by the single digit value so the output would put the 2's(or highest number) to the top

Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[415891] => 2
[125313] => 2
[67376] => 2
[342520] => 2
[357301] => 2
[33636] => 1
[73250] => 1
[142062] => 1
[357303] => 1
[404419] => 1
[408957] => 1
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)
1
  • 2
    FYI, thats not the "second value". That's the "value", and the big numbers, like 1845267, are the "key"s. Commented Sep 19, 2011 at 15:54

4 Answers 4

4

Try with the arsort function:

arsort — Sort an array in reverse order and maintain index association

Example:

arsort($array);
// done, $array is now sorted
Sign up to request clarification or add additional context in comments.

2 Comments

I am using the system below to generate the array above, when I attempt to print_r(arsort($weights)); the output is simply the digit 1. $weights = array_count_values($weight); print_r(arsort($weights));
arsort() doesn't return the array, it sorts the array in place. Do arsort($weights); print_r($weights);
1

Built into PHP: http://php.net/manual/en/function.arsort.php

Comments

0

bascially you need toh known array function for php from here

and function as
arsort($arr);

Comments

0

Simple

arsort($array)

http://www.php.net/manual/en/function.arsort.php

Comments

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.