Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    135
    Thanks
    1
    Thanked 0 Times in 0 Posts

    having problems with sort();

    I'm having trouble with sort(); I can't seem to get it to work...

    Now, I just typed this up... Just psedocode, although it *might* actually work! lol! Do I have the right idea? I haven't had any luck getting sort to do anything yet... Do I need to create a multidimensional array or something?

    And no, I can't use ASC, or DESC!

    Code:
    	
    //Nearly the exact same as a normal MySQL query, EXCEPT I CANT USE ASC, or DESC!!!!!
    $friends = $facebook->api_client->fql_query("SELECT uid, name, pic_square FROM user");
    
    $uids = array();
    $names = array();
    $pics = array();
    
                foreach ($friends as $friend) {
                 push($uids, $friend['uid']);
                 push($names, $friend['name']); //The data is all strings just like a normal name, eg:    John Smith
                 push($pics, $friend['pic_square']);
        }
    
    //Now, can I just use this sort to sort the names alphabetically
    //and keep the uid's and pics in-line with the name like this?
    
    sort($names,$uids,$pics);
    
    //Now, this should return the three correct
    // values for the person with the highest alphabetical name... Eg: Andrew Smith
    echo $names[0] . $uids[0] . $pics[0];
    
    //This would return the second, say there was only one person who's name started with a, and one with b.
    echo $names[1] . $uids[1] . $pics[1];
    //Would return Bart Smith, his uid, and pic.
    Last edited by iceflyin; 08-28-2007 at 01:39 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You're separating a mulit-dimensional array into three separate arrays, sorting one of those new arrays, and exactly how do you expect the other two arrays to magically get sorted in the same order as the one array you're sorting?

    No, that won't work. You shouldn't create three different arrays. Simply sort the multi-dimensional array by name. Here's a pretty easy way to do that:

    PHP Code:

    //create temporary array based on the array element you want to sort by
    foreach ($friends as $key => $val) {
        
    $sortValues[$key] = $val['name'];
    }

    //sort the temporary array
    asort ($sortValues);

    //build sorted array based on original array, in the order of temporary array
    reset ($sortValues);
    while (list (
    $arrayKey$arrayVal) = each ($sortValues)) {
        
    $sortedArray[] = $friends[$arrayKey];
    }

    //assign sorted array to original array
    $friends $sortedArray
    Last edited by Fumigator; 08-28-2007 at 01:44 AM.

  • Users who have thanked Fumigator for this post:

    iceflyin (08-28-2007)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •