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 14 of 14
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array sorting by third key

    Hey,

    I ahve this array I was wondering if someone could help me sort:

    Code:
    Array
    (
        [administration] => Array
            (
                [admin_main] => Array
                    (
                        [2] => Array
                            (
                                [section_path] => content/
                            )
    
                    )
    
                [admin_reports] => Array
                    (
                        [1] => Array
                            (
                                [section_path] => content/
                            )
    
                    )
    
            )
    }
    I need to be able to sort it by the third key. As you see 2,1. It need to be 1,2. This is not the full array as it would take up a long page. The administration key is not always administration.

    Thanks in advance.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If I understand what you are looking for, you are trying to sort then entire array's administration association based on the first level key value of the admin_main or admin_reports correct?
    Use a uasort:
    PHP Code:
    function cmpAdministrationASC($a$b)
    {
        
    $fv key($a);
        
    $sv key($b);
        return 
    $fv $sv;
    }

    uasort($array['administration'], 'cmpAdministrationASC'); 

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey,

    Thanks for the reply.

    Here is the full array:

    Code:
    Array
    (
        [administration] => Array
            (
                [admin_main] => Array
                    (
                        [2] => Array
                            (
                                [section_path] => content/
                            )
                            
                        [1] => Array
                            (
                                [section_path] => content/
                            )
    
                    )
    
                [admin_reports] => Array
                    (
                        [3] => Array
                            (
                                [section_path] => content/
                            )
                            
                        [1] => Array
                            (
                                [section_path] => content/
                            )
    
                    )
    
            )
            
        [another] => Array
            (
                [soemthing_else] => Array
                    (
                        [2] => Array
                            (
                                [section_path] => content/
                            )
                            
                        [7] => Array
                            (
                                [section_path] => content/
                            )
    
                    )
    
                [something] => Array
                    (
                        [5] => Array
                            (
                                [section_path] => content/
                            )
                            
                        [2] => Array
                            (
                                [section_path] => content/
                            )
    
                    )
    
            )
    }
    basically I need to order the 3rd level by the numbers. Any ideas mate?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    But keep the rest of the items in place?
    Its doable, but I don't think I'd recommend the approaches since they are all either iterative, recursive, or direct pull of each element out of the array. How is this array created in the first place; that is likely where it would be best to sort the last level instead of waiting until after the array is fully constructed.

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey,

    This is a menu system. It is pointless me pasting the code as it references classes which you would need to look through about 3 class files.

    Would you be able to help me sort this mate?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If these are object that have been decayed into an array you can write a comparator for the objects themselves and then invoke a usort prior to the decay cast. If they are not objects at the point they have been added to an array, since these all have associative and unknown associative offsets, then you'll need to write an entire sort for it, which will be complex since it needs to get to the third level of unknown offsets.
    The easiest solution is to go back to where the last level is added and use a ksort on it at that point.

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah ok.

    Here is the part which loops the folders and creates the array:

    PHP Code:
    $full_path $section_array['path'];
                    
                    if (
    $handle opendir($full_path)) {
                        while (
    false !== ($folder readdir($handle))) {
                            if(
    is_dir($full_path.$folder) && $folder != '.' && $folder != '..' && $folder != 'cmd'){
                                
                            
                                
                                
    $this->content_group_array[$section_array['folder']][$folder][$this->config_file_array[$folder]['sort']] = array(
                                
                                                                                                    
    'folder' => $folder,
                                                                                                    
                                                                                                    
    'name' => $this->config_file_array[$folder]['name'],
                                                                                                    
    'icon' => $full_path.$folder.'/icon.png',
                                                                                                    
                                                                                                    
    'link' => $section_array['folder'].'/'.$folder.'/',
                                                                                                    
    'path' => $full_path.$folder.'/'
                                                                                                    
                                                                                                    
    ); 
                            }
                        }
                    } 
    Where would I put it for this, would you know?

    The code above is inside a loop so I guess it would be somewhere outside this code?

    Thanks

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Okay, I see what you are doing here.
    Here's what you need to do. We need to bring an array up to the level of the 'folder'. We don't care what this key is since it will be iterated and sorted after. It *can* be sorted within the loop, but I recommend against this for two reasons: its more complicated, and using a sort within a location that modifies the data itself can often lead to issues such as infinite loops.
    PHP Code:
    if ($handle opendir($full_path)) {
    $aFolders = array();
                        while (
    false !== ($folder readdir($handle))) {
                            if(
    is_dir($full_path.$folder) && $folder != '.' && $folder != '..' && $folder != 'cmd'){
                                
                            
                                
                                
    $aFolders[$this->config_file_array[$folder]['sort']] = array(
                                
                                                                                                    
    'folder' => $folder,
                                                                                                    
                                                                                                    
    'name' => $this->config_file_array[$folder]['name'],
                                                                                                    
    'icon' => $full_path.$folder.'/icon.png',
                                                                                                    
                                                                                                    
    'link' => $section_array['folder'].'/'.$folder.'/',
                                                                                                    
    'path' => $full_path.$folder.'/'
                                                                                                    
                                                                                                    
    ); 
                            }
                        }
                        
    ksort($aFolders);
                        
    $this->content_group_array[$section_array['folder']] = array_merge($this->content_group_array[$section_array['folder']], $aFolders);

    So if I understand the structure you have here, this appears that it will work. I assumed that $section_array is also a grouping level of some sorts which would indicate a loop level above this somewhere.

  • #9
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey,

    Sorry fi this takes your time up mate. I really can't thank you enough for helping me. I did put in the code but it through an error at me.

    Here is the full function below.

    PHP Code:
    function structure_sections_content_group_array(){
            
            foreach(
    $this->section_array as $section_sort){
                
                foreach(
    $section_sort as $section_section => $section_array){
                
                    
    $full_path $section_array['path'];
                    
                    if (
    $handle opendir($full_path)) {
                        while (
    false !== ($folder readdir($handle))) {
                            if(
    is_dir($full_path.$folder) && $folder != '.' && $folder != '..' && $folder != 'cmd'){
                                
                                
    $this->structure_read_config_file('content_group.config'$full_path.$folder$folder);
                                
                                
    $this->content_group_array[$section_array['folder']][$folder][$this->config_file_array[$folder]['sort']] = array(
                                
                                                                                                    
    'folder' => $folder,
                                                                                                    
                                                                                                    
    'name' => $this->config_file_array[$folder]['name'],
                                                                                                    
    'icon' => $full_path.$folder.'/icon.png',
                                                                                                    
                                                                                                    
    'link' => $section_array['folder'].'/'.$folder.'/',
                                                                                                    
    'path' => $full_path.$folder.'/'
                                                                                                    
                                                                                                    
    ); 
                            }
                        }
                    }
                    
                }
                
                
    //usort($this->content_group_array);
            
    }
            
            
    //echo'CONTENT GROUP ARRAY <pre>'; print_r($this->content_group_array); echo'</pre>';
            
    $this->config_file_array = array();
        } 
    $this->section_array has an array like this:

    Code:
    Array
    (
        [administration] => Array
            (
                [100] => Array
                    (
                        [folder] => administration
                        [name] => Admin
                        [link] => administration/
                        [path] => content/administration/
                    )
    
            )
    
        [catalogue] => Array
            (
                [2] => Array
                    (
                        [folder] => catalogue
                        [name] => Catalogue
                        [link] => catalogue/
                        [path] => content/catalogue/
                    )
    
            )
    
    )
    Thanks for your help.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    These outputs are all becoming different. Section array doesn't follow the same format as your previous arrays.
    What is the error you had?

  • #11
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey,

    This is a menu structure.

    The section_array is the top level.
    Then you have content_group_array which is the second level
    and finally you have content_array which is the third and last level ( I haven't mentioned this before)

    This is the error I get:

    Error: [8] Undefined index: administration
    Error in C:\Zend\Apache2\htdocs\admin\inc\class\system\structure.php on line 121 : Undefined index: administration
    Last edited by blinn_shade; 01-28-2012 at 09:39 PM.

  • #12
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or instead of the the last key being the number I can put it in the array like this if it would work better?

    Code:
    Array
    (
        [administration] => Array
            (
                [admin_main] => Array
                    (
                       
                                [section_path] => content/
                                [sort] => 2
                          
    
                    )
    
                [admin_reports] => Array
                    (
                        
                                [section_path] => content/
    							[sort] => 1
                            
    
                    )
    
            )
    }

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, modifying the structure doesn't look like it will work. You have multiple section_path below admin_main, so that has to be an array.
    Also, I appear to have biffed this as well. I don't think this should array_merge since the array is full and complete after the loop, and doesn't look like section_array[folder] section exists before this point.
    Change this:
    PHP Code:
    $this->content_group_array[$section_array['folder']] = array_merge($this->content_group_array[$section_array['folder']], $aFolders); 
    to this:
    PHP Code:
    $this->content_group_array[$section_array['folder']] = $aFolders

  • #14
    New Coder
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    THanks mate. I decided not to order them now as the folder names order it anyway. Sorry if this troubled you.


  •  

    Posting Permissions

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