views:

561

answers:

6

Hi guys,

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!

A: 

You might consider array_walk and array_walk_recursive and array_map, which when combined together maybe to get to doing what you want to do.

Kitson
A: 

Try with array_mulisort.

hsz
+6  A: 
Dereleased
Hi! Awesome stuff. Thanks for your help!I am not sure that i have explained myself properly though.I want to check if the values inside the "countries"-array contains a set value (like "EN") and if so, raise the priority in sorting on the parent array.
Industrial
Big thanks to you Dereleased!
Industrial
I just added a huge amount of info on a numerical user-defined sorting mechanism that you might like, please check it out and let me know what you think!
Dereleased
Jeez man, you're amazing! I dont know what to say. Thanks!Definitely need to try this solution out! I owe you one for this! Thanks again!
Industrial
Had help of this again. You're the man, Dereleased! :)
Industrial
A: 

Check out uasort to see how to use a user defined comparison function.

Thilo
Nvm, Dereleased was faster :)
Thilo
A: 

Hi again! 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?

Industrial
+1  A: 

Finally found this wonderful function at PHP.net:

        function array_msort($array, $cols)
        {
            $colarr = array();
            foreach ($cols as $col => $order) {
                $colarr[$col] = array();
                foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
            }
            $eval = 'array_multisort(';
            foreach ($cols as $col => $order) {
                $eval .= '$colarr[\''.$col.'\'],'.$order.',';
            }
            $eval = substr($eval,0,-1).');';
            eval($eval);
            $ret = array();
            foreach ($colarr as $col => $arr) {
                foreach ($arr as $k => $v) {
                    $k = substr($k,1);
                    if (!isset($ret[$k])) $ret[$k] = $array[$k];
                    $ret[$k][$col] = $array[$k][$col];
                }
            }
            return $ret;

        }

This is how each country looks like: $array['countries'] = in_array($needle, $haystack); }

$array = $array = array_msort($array, array('countries'=>SORT_DESC, 'id'=>SORT_ASC));

Thanks all for your help!

Industrial