Assistance Sorting Database Result Set

Hello.

I’ve rtfm’d on php.net, and I’ve also searched here but have not found a solution to my issue.

I have result sets from several MySQL queries (on different servers or I would solve this in the query ORDER BY clause), and they are combined into 1 php array which takes the form:

Array
(
    [0] => Array
        (
            [SITE] => Site A
            [FREQUENCY] => 8
            [SITEID] => 24121
        )

    [1] => Array
        (
            [SITE] => Site B
            [FREQUENCY] => 14
            [SITEID] => 29001
        )

    [2] => Array
        (
            [SITE] => Site C
            [FREQUENCY] => 4
            [SITEID] => 18027
        )
)

I need to sort the array on the FREQUENCY field descending.

I’ve had no success with arsort() or array_multisort().

Can anyone offer guidance as to how I might accomplish this with the built-in php functions?

You don’t need to return -1 if $a is smaller, any negative value will do, just as any positive value will do if $b is smaller, only 0 for equality is fixed.

That being said, this also works:


usort($data,create_function('$a,$b','return $b["FREQUENCY"]-$a["FREQUENCY"];'));

:slight_smile:


<?php
$data = array(
	array('FREQUENCY'=>8)
	,array('FREQUENCY'=>14)
	,array('FREQUENCY'=>4)
);

usort($data,create_function('$a,$b','return $a["FREQUENCY"] != $b["FREQUENCY"]?$a["FREQUENCY"]>$b["FREQUENCY"]?-1:1:0;'));
echo '<pre>',print_r($data),'</pre>';
?>

usort

This definitely puts me on the right track.

Thank you for the feedback.