9

I would like to sort the following names

Array ( [Jessie] => 2 [Sarah] => 3 [Simon] => 2 [John] => 2 [Kevin] => 1 [Canvasser] => 8 [canvasser] => 11 )

based on the values corresponding to them

I printed the names through the following function

// get canvasser individual names and count houses canvassed
    foreach ($canvassers as $key => $value) {
        // Add to the current group count if it exists
        if ( isset( $canvasser_counts[$value] ) ) {
            $canvasser_counts[$value]++;
        }
        // or initialize to 1 if it doesn't exist
        else {
            $canvasser_counts[$value] = 1;
        }
    }
    print_r($canvasser_counts);

where $canvassers simply held all the names eg.

$canvassers = array('Jessie', 'Simon', 'Jessie')

Any help would be really appreciated, I have spent so long on this but can't get my head straight to sort the array correctly.

3
  • asort might help here: php.net/manual/en/function.asort.php but I am unsure of what ordering you want, maybe you can define that
    – Sammaye
    Sep 22, 2012 at 21:18
  • the question is to sort "based on the values corresponding to them," so asort() is the correct way to do this. Sep 22, 2012 at 21:23
  • @doublesharp What if he wants to sort in arsort() "based on the values corresponding to them," ?
    – Sammaye
    Sep 22, 2012 at 21:25

1 Answer 1

13

You want to use asort() - http://php.net/manual/en/function.asort.php - to sort the values in ascending order, or arsort() - http://php.net/manual/en/function.arsort.php - to sort in descending order.

Given this PHP:

$vals = array("Jessie" => 2, "Sara" => 3, "Simon" => 2, "John" => 2, "Kevin" => 1, "Canvasser" => 8, "canvasser" => 11 );
print_r($vals); // current order
asort($vals); // sort array
print_r($vals); // new order

You will get the following output:

Array
(
    [Jessie] => 2
    [Sara] => 3
    [Simon] => 2
    [John] => 2
    [Kevin] => 1
    [Canvasser] => 8
    [canvasser] => 11
)
Array
(
    [Kevin] => 1
    [Jessie] => 2
    [John] => 2
    [Simon] => 2
    [Sara] => 3
    [Canvasser] => 8
    [canvasser] => 11
)
6
  • The accuracy of this answer is highly dependant upon WHAT order he wants, which he has not actually defined so I am not sure why it is getting upvoted so quickly. I mean, fair enough there is a 90% chance he wants asc but still...
    – Sammaye
    Sep 22, 2012 at 21:22
  • 1
    Fair enough - if you want the opposite, you can use arsort() (php.net/manual/en/function.arsort.php) or pass the array to array_reverse() after it is sorted - php.net/manual/en/function.array-reverse.php Sep 22, 2012 at 21:25
  • Don't forget usort() as well, he might be looking for a custom sort as well based upon teh values.
    – Sammaye
    Sep 22, 2012 at 21:29
  • Hi thank you so much for your reply. Both of you! However, I would like to sort by numerical values so in the example above, 'canvasser' would be first, 'Canvasser' second, 'Sara' third etc. Do you know of a way to do this please?
    – dev
    Sep 22, 2012 at 22:01
  • Ha, looks like you were the 10%. You want to sort by arsort() to get the reverse of the example above. Sep 22, 2012 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.