...

View Full Version : Array Sorting (multidimensional)



rswyatt
01-23-2007, 04:52 PM
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?

Fumigator
01-23-2007, 10:35 PM
I'm trying to make sense out of the array you listed... it looks like this to me:



[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?



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:



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.

rswyatt
01-25-2007, 01:23 PM
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!

Fumigator
01-25-2007, 05:37 PM
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?



[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...

rswyatt
01-25-2007, 09:31 PM
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.

Fumigator
01-25-2007, 11:12 PM
Simply build the array to keep each group of data on the same first order index so that it looks like this:



[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:


//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;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum