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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Array Sorting (multidimensional)

    I have an array:

    array(2) { [0]=> array(1) { ["pageID"]=> string(1) "8" } [1]=> array(1) { ["thematch"]=> float(1.34) } [2]=> array(1) { ["table"]=> string(2) "ct" } [3]=> array(1) { ["pageID"]=> string(1) "6" } [4]=> array(1) { ["thematch"]=> float(1.33) } [5]=> array(1) { ["table"]=> string(2) "ct" } }

    The problem - I want to sort this array by the second level key value of "thematch".

    I've tried using the various sort, ksort, asort etc but to no avail.

    Can someone suggest a way to sort this array by the value (in descending order) of that key?
    Rich S. Wyatt
    D3 Web Creations

  • #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
    I'm trying to make sense out of the array you listed... it looks like this to me:

    Code:
    [0] =>	
    		['pageID'] => '8'
    [1] =>	
    		['thematch'] => 1.34
    [2] =>	
    		['table'] => 'ct'
    [3] =>	
    		['pageID'] => '6'
    [4] =>	
    		['thematch'] => 1.33
    [5] =>	
    		['table'] => 'ct'
    So to assign those values it would look like this?

    Code:
    myArray[0]['pageID'] = '8';
    myArray[1]['thematch'] = 1.34;
    myArray[2]['table'] = 'ct';		
    myArray[3]['pageID'] = '6';
    myArray[4]['thematch'] = 1.33;
    myArray[5]['table'] = 'ct';
    Maybe I'm just not familiar with the notation you used, but that doesn't look right. If it were this:

    Code:
    myArray[0]['pageID'] = '8';
    myArray[0]['thematch'] = 1.34;
    myArray[0]['table'] = 'ct';		
    myArray[1]['pageID'] = '6';
    myArray[1]['thematch'] = 1.33;
    myArray[1]['table'] = 'ct';
    Then I have some idea of how to sort that on the ['thematch'] index.

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are correct - I didn't <pre>print_r</pre> it for you. Sorry. :-(

    Anyhow - that is indeed how it is set up. What I'm doing is a MATCH AGAINST MySQL query (howbeit several of them to handle several different tables) and I'm writing them into a 'master' array. I've pre-sorted thematch by query - but as they get written into the array - it isn't nicely feasible to order them. After the array is completely written - I'd like to go through it and sort by 'thematch'...

    I'm interested in hearing your though!
    Rich S. Wyatt
    D3 Web Creations

  • #4
    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
    Ok.... hmmmm... so you are incrementing the first order of the array for every bit of data that goes into the array. I just can't wrap my brain around what that would do for you. Sorting by ['thematch'] would then only sort 1/3 of the array, since 2/3 of the array doesn't have a ['thematch'] index. So the end result would look like this?

    Code:
    [0] =>	
    		['thematch'] => 1.33
    [1] =>	
    		['thematch'] => 1.34
    [2] =>	
    		['pageID'] => '8'
    [3] =>	
    		['table'] => 'ct'
    [4] =>	
    		['pageID'] => '6'
    [5] =>	
    		['table'] => 'ct'
    My problem with your array is this: It doesn't group the data, and it seems like you'd want it to group a pageID, a thematch, and a table together, so you could refer to myArray[0]['thematch'] and know that myArray[0]['pageID'] is the pageID of that thematch. As it is, myArray[1]['thematch'] has a pageID stored in myArray[0]['pageID'], but we only know that because we assume it to be true. PHP won't know that, and re-sorting the array will blow that tenuous connection out of the water.

    If I've gone off on the wrong line of thinking, set me straight...

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you're right - I think I'm building this array incorrectly. What a dope! :-)

    Perhaps I should be giving the primary key a different value? When it comes to the end - I want to keep all the data grouped but sort the entire thing in order of each data's thematch value.

    The problem is - I'm having a hard time grasping how I should build this array differently to achieve the result that I want. I think my brain is toasty.
    Rich S. Wyatt
    D3 Web Creations

  • #6
    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
    Simply build the array to keep each group of data on the same first order index so that it looks like this:

    Code:
    [0] =>	
    		['pageID'] => '8'
    		['thematch'] => 1.34
    		['table'] => 'ct'
    [1] =>	
    		['pageID'] => '6'
    		['thematch'] => 1.33
    		['table'] => 'ct'
    After you have the array fixed, you can sort it by a second order index like this:
    PHP Code:
    //build a temporary array containing just the sort values
    foreach($myArray as $i => $val) {
        
    $sortValues[$i] = $val['thematch'];
    }

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

    //build a COPY of the original array in the order of the sorted temporary array
    while (list ($arrKey$arrVal) = each ($sortValues)) {
        
    $sortedArray[] = $myArray[$arrKey];
    }

    //assign the COPY to the original
    $myArray $sortedArray


  •  

    Posting Permissions

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