How do I count and sort simple multidimensional array like this?

Discussion in 'PHP' started by peppy, May 24, 2011.

  1. #1
    Greetings,

    I have an array like this:

    Array
    (
    [0] => Array
    (
    [userid] => 1
    [name] => Joe1
    )

    [1] => Array
    (
    [userid] => 1
    [name] => Joe1
    )

    [2] => Array
    (
    [userid] => 2
    [name] => Joe2
    )

    [3] => Array
    (
    [userid] => 3
    [name] => Joe3
    )

    [4] => Array
    (
    [userid] => 2
    [name] => Joe2
    )

    [5] => Array
    (
    [userid] => 2
    [name] => Joe2
    )

    )

    The userid corresponds to a name. I would like to count up how many times the userid appears in the array. Then I would like to sort the array where the userid with the highest count appears first and desends. Basically this is what I want the array to look like:

    Array
    (
    [0] => Array
    (
    [userid] => 2
    [name] => Joe2
    [count] => 3
    )

    [1] => Array
    (
    [userid] => 1
    [name] => Joe1
    [count] => 2
    )

    [2] => Array
    (
    [userid] => 3
    [name] => Joe3
    [count] => 1
    )

    )

    Let me know how this can be done.

    Thanks
     
    peppy, May 24, 2011 IP