Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    multi dimensional array sort

    Hi guys,



    I'm now having sorting the MD array...

    PHP Code:
    $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 have the following to sort it

    array_multisort($metric_array[3], SORT_DESC, SORT_NUMERIC);

    yet it still doesnt do anything! I expect the element at the end, (is_high_level) where it's 1 to be at the top??

    ALSO......

    What I need to do is to set all the "is_high_level" elements = 0 where the first array element is X.

    eg: x=1 would go through and set all the is_high_level elements to 0. I can then update the indiviual array element later.

    should I just loop through and use a where??
    Last edited by homerUK; 11-22-2007 at 11:52 AM.

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    still not having any luck - anyone able to help?

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Here's how I sort a multi-dimensional array:
    PHP Code:

    //Make a single-index array based on the index from the multi-array you want to sort on
    foreach ($arr as $key => $val) {
        
    $sortSingle[$key] = $arr[$key]['sortIndex'];
    }

    //Sort the single-index array
    asort ($sortSingle);

    //Build new multi-array based on original multi-array in the order of the single-index array
    reset ($sortSingle);
    while (list (
    $singleKey$singleVal) = each ($sortSingle)) {
        
    $newArr[] = $arr[$singleKey];
    }

    //Assign the new array to the original array
    $arr $newArr

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    I tried this code from your example:

    PHP Code:
    $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



    foreach (
    $arr as $key => $val) { 
        
    $sortSingle[$key] = $arr[$key]['is_high_level']; 


    //Sort the single-index array 
    asort ($sortSingle); 

    //Build new multi-array based on original multi-array in the order of the single-index array 
    reset ($sortSingle); 
    while (list (
    $singleKey$singleVal) = each ($sortSingle)) { 
        
    $newArr[] = $arr[$singleKey]; 


    //Assign the new array to the original array 
    $arr $newArr;
    echo 
    "<pre>";
    print_r($arr);
    echo 
    "</pre>"
    which gave me

    Code:
    Array
    (
        [0] => Array
            (
                [2.01] => Array
                    (
                        [R21101] => Array
                            (
                                [is_high_level] => 1
                            )
    
                    )
    
                [2.02] => Array
                    (
                        [R21102] => Array
                            (
                                [is_high_level] => 0
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [1.01] => Array
                    (
                        [R11101] => Array
                            (
                                [is_high_level] => 0
                            )
    
                    )
    
                [0.00] => Array
                    (
                        [R11100] => Array
                            (
                                [is_high_level] => 0
                            )
    
                    )
    
                [1.02] => Array
                    (
                        [R11102] => Array
                            (
                                [is_high_level] => 1
                            )
    
                    )
    
                [1.03] => Array
                    (
                        [R11103] => Array
                            (
                                [is_high_level] => 0
                            )
    
                    )
    
            )
    
    )
    which appears it's not working... any ideas?! The high_level where it's 1 needs to be the first element in the array..... trouble is, it THEN needs sorting on the decimal number like 1.01 then 2.01 etc.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •