+ Reply to Thread
Results 1 to 3 of 3

Thread: Array sorting in php

  1. #1
    drf1229 is offline x10Hosting Member drf1229 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    71

    Array sorting in php

    I'm trying to sort two arrays in php. One is full of strings and another integers. What I want to do is sort the integers by numeric order and then sort the strings the exact same way as integers. The reason it needs to be so perfect us because the first array is a set of usernames, the second is a set of scores, so they correspond like this: $firstarray[1] is the name for $secondarray[1]. I'm trying to sort out the scores by numeric order. Any suggestions for how to do this? Any advice is greatly appreciated!
    Last edited by drf1229; 01-06-2010 at 08:22 PM.
    Your 14 year old web developer. If I can do it, you can!
    My site
    My iPhone App

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Array sorting in php

    Two options: use array_multisort or combine the arrays with array_combine and sort the resulting array using asort or arsort. You'll need to use the usernames as keys, since scores may not be unique.

    PHP Code:
    // option 1:
    array_multisort($scores$users);

    // option 2:
    $userscores array_combine($users$scores);
    asort($userscores); 
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    drf1229 is offline x10Hosting Member drf1229 is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    71

    Re: Array sorting in php

    Thank you so much! You're a lifesaver!
    Your 14 year old web developer. If I can do it, you can!
    My site
    My iPhone App

+ Reply to Thread

Similar Threads

  1. [PHP] Search a multidimensional array
    By diabolo in forum Programming Help
    Replies: 14
    Last Post: 01-07-2010, 01:18 PM
  2. [REQ] [200] PHP create array
    By diabolo in forum The Marketplace
    Replies: 3
    Last Post: 12-15-2008, 02:39 PM
  3. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM
  4. [PHP] sorting 2-dimensional array by timestamp-key
    By bonzo meier in forum Programming Help
    Replies: 0
    Last Post: 02-19-2008, 04:28 PM
  5. Can some1 tell me if this is the legal IF 2.0?
    By pensoftware in forum Computers & Technology
    Replies: 9
    Last Post: 04-05-2006, 12:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers