multidim array sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abhi

    multidim array sort

    Hi!

    I am wondering if someone can point me to the needed sort function. There are so many of
    them that I simply got lost by reading their descriptions. I try to explain what I need.

    I have a multidimensiona l array and I have a function that outputs it as a table.

    $arr[0] contains an array that contains usernames
    $arr[1] contains an array that contains real names
    $arr[2] contains an array that contains e-mails
    etc...
    $arr[last] contains integers for users "levels"

    My function outputs users with their datas in the columns. Each column means one user.

    Now I want to sort this whole array by levels. So, all other data should be sorted
    according to the pattern in levels row.

    How is this thing done?

    Please advice!


    Abhi


  • Erwin Moller

    #2
    Re: multidim array sort

    Abhi wrote:
    [color=blue]
    > Hi!
    >
    > I am wondering if someone can point me to the needed sort function. There
    > are so many of them that I simply got lost by reading their descriptions.
    > I try to explain what I need.
    >
    > I have a multidimensiona l array and I have a function that outputs it as a
    > table.
    >
    > $arr[0] contains an array that contains usernames
    > $arr[1] contains an array that contains real names
    > $arr[2] contains an array that contains e-mails
    > etc...
    > $arr[last] contains integers for users "levels"
    >
    > My function outputs users with their datas in the columns. Each column
    > means one user.
    >
    > Now I want to sort this whole array by levels. So, all other data should
    > be sorted according to the pattern in levels row.
    >
    > How is this thing done?
    >
    > Please advice!
    >
    >
    > Abhi[/color]

    Hi Abhi,

    You need array_multisort .

    Here follows a basic example:

    -----------------------------------
    <?
    $arr["username"] = array("John" , "Bert" , "Theo" , "Peter");
    $arr["realname"] = array("Johnny Bravo" , "Bert Visser" , "Theo Maasen" ,
    "Peter Hammil");
    $arr["email"] = array("john@hot mail.com" , "bert@hotmail.c om" ,
    "theo@yahoo.com " , "peer@gmail.com ");
    $arr["level"] = array( 8 , 5 , 3 , 5);
    ?>
    Before:
    <pre>
    <? print_r($arr); ?>
    </pre>
    <?
    // sort
    array_multisort ($arr["level"] , $arr["username"] , $arr["realname"] ,
    $arr["email"]);
    ?>
    <hr>
    After:
    <pre>
    <? print_r($arr); ?>
    </pre>

    -------------------------------

    When using array_multisort remember:
    1) Your original arrays (the ones you feed to the function) will change
    order, so no 'resultarray'. The function just return true on succes and
    false on failure.
    2) The ordering will be done in order of assigned arrays.
    Or to put it more easily: look at the passed arrays as COLUMNS for a table.

    Read it all at php.net.

    Regards,
    Erwin Moller

    Comment

    Working...