Hi Guys,

I am having trouble sorting this array

$arr [1]["1.01"]["R11101"]["is_high_level"] = 0;   
$arr [1]["0.00"]["R11100"]["is_high_level"] = 0;
$arr [1]["1.02"]["R11102"]["is_high_level"] = 1;
$arr [1]["1.03"]["R11103"]["is_high_level"] = 0;
$arr [2]["2.01"]["R21101"]["is_high_level"] = 1;
$arr [2]["2.02"]["R21102"]["is_high_level"] = 0;

I need to sort by the $arr[3] element first (is high level), then by the $arr[1] element. Am I going about building the array incorrectly, ie: should I be using an array_push to generate it? I need to get the results from the DB then using another query, update element in the array before I put it out on screen.

Thanks for any info!

    I've tried using that:

    function cmp($a, $b){
    	if($a[3]==$b[3]) return 0;
    			return ($a[3]>$b[3]) ? -1 : 1;
    }
    
    usort($arr,'cmp');
    

    which seems to have no effect!

      That's because $arr[3] is the fourth element of the $arr array. You are looking to sort by dimensions, from what I see. I am not too clear on your array structure. How exactly is your array structured?

      Alternatively, there is [man]array_multisort/man, but generally I would try for usort() first.

        mfacer wrote:
        if($a[3]==$b[3]) return 0;

        It also fails to take into account the fact that, if comparing values in column 3 isn't enough to get values in the right order, you want to use column 1 to refine the sort.

          thanks for the replies...
          I chose the array structure above so that I could reference elements by their ID number. Maybe this is incorrect. I may try something like

          array("hierarchy"=>$hierarchy, "process"=>$process, "metricID"=>$metricID, "is_high_level"=$is_high_level);

          essentially, what I need to do is get out of the database results and order them in their hierarchy, eg:

          1	1.00	R0000	1
          1	1.01	R0001	0	//hidden
          1	1.02	R0002	0	//hidden
          1	1.03	R0003	0	//hidden
          2	2.02	F0002	1
          2	2.00	F0000	0	//hidden
          2	2.01	F0001	0	//hidden
          

          so I would loop through the array and show all the "is_high_level" elements, then all the ones under it would be hidden (using div style none) then once i get to another is_high_level element, show that etc.

          It's ruddy hard to explain really, sorry!!!

            Sorting the array using usort is still an option, but if you're getting these results from a database query, why not specify the order as part of the query?

              I get the results, but then another query to the database will update and change what is essentially the "high_level". Unless I do a join somehow...

                Write a Reply...