I am currently creating a sorting method that consists of values from an mysql query.
Here’s a brief view of the array:
Array
(
[0] => Array
(
['id'] = 1;
['countries'] = 'EN,CH,SP';
)
[1] => Array
(
['id'] = 2;
['countries'] = 'GE,SP,SV';
)
)
I have succeeded in making a normal usort based on the numeric id values, but I rather want to sort the array by the content of the “countries” field (if it contains a set string, a country code in this case), and then by the id field.
The following snippet was my first idea of how to do it, but I have no idea of how to incorporate it into an working function:
in_array('EN', explode(",",$a['countries']) );
How would you do it?
Thanks!
I am really getting nowhere with this unfortunately.
Here is what I have for the moment, and its giving me nothing but errors: uasort() [function.uasort]: Invalid comparison function
function compare($a, $b) {
global $usercountry;
if ( in_array($usercountry, $a['countries']) && in_array($usercountry, $a['countries']) ) {
$return = 0;
}
else if (in_array($usercountry, $a['countries'])) {
$return = 1;
}
else {
$return = -1;
}
return $return;
}
$array= usort($array, "compare");
Is there anyone who might give me a hint of how to go on with it?
Personally, I would use a custom (anonymous) function in conjunction with
usort().EDIT: Re – your comment. Hopefully this will put you on the right track. This function gives equal priority to elements which both have EN or neither have EN, or adjusted priority when just one has EN.
This function, on the other hand, gives equal priority if both have EN, higher if one has EN, and does a text comparison if neither has EN.
Again, hopefully this will give you enough direction to move forward on your own. If you are having any problems, feel free to post more comments and I’ll try to help.
A note if you’re tying to compare multiple properties with weight: try out a funky switch block, e.g.#More Edits!#
Actually, I was thinking more on the problem of complex sorting, and I have come up with the following solution, for your consideration. It will allow you to define numerical rankings based on keywords which would appear in the countries index. Here is the code, including an example:
Example Array
Sorting Routine
Note: This code will sort the highest ranking entires to the top of the array. If you want reverse behavior, change this:
to
Note that the greater-than was changed to a less-than symbol. Making this change will result in the lowest ranking entries being sorted to the top of the array. Hope all this helps!
##NOTE ALSO!##
This code will make a small change to your array, in that it adds the _score element to each array. Hopefully this is not a problem, as by storing this value I was literally able to increase speed by more than double (.00038-.00041 down to .00016-.00018 in my benchmarks). If not, remove the
ifblocks that retrieve the cached value and let the contents of theelseblocks execute every time, except of course for the part which stores the score value.By the way, here’s a
var_export()dump of the array after it was sorted:Enjoy!