Search code examples
phparraysmultidimensional-arrayhierarchical

Convert a flat array of delimited values into a hierarchical array


I am working with a permission system where I have to convert a single level array (separated by underscores) to a multidimentional array. Any help with a function that fix this?

Input array:

Array
(
    [0] => dashboard
    [1] => dashboard_read
    [2] => dashboard_update
    [3] => dashboard_create
    [4] => dashboard_delete
    [5] => dashboard_search
    [6] => timeplan_event_read
    [7] => timeplan_event_search
    [8] => timeplan_read
    [9] => timeplan_search
    [10] => webhotel
    [11] => webhotel_read
    [12] => webhotel_update
    [13] => webhotel_create
    [14] => webhotel_delete
    [15] => webhotel_search
)

Output array:

array(
    'dashboard' => array(
        'read',
        'update',
        'create',
        'delete',
        'search'
    ),
    'timeplan' =>array(
        'read',
        'search',
        'event' => array(
            'read',
            'search'
        )
    ),
    'webhotel' =>array(
        'read',
        'update',
        'create',
        'delete',
        'search'
    ),
)

Solution

  • [akshay@localhost tmp]$ cat test.php
     <?php
    
     $array = array("dashboard","dashboard_read","dashboard_update","dashboard_create","dashboard_delete","dashboard_search","timeplan_event_read","timeplan_event_search","timeplan_read","timeplan_search","webhotel","webhotel_read","webhotel_update","webhotel_create","webhotel_delete","webhotel_search");
    
    
     function build_arr($array, $delim='_') 
     { 
       $output = array();
       foreach($array as $key)
       {
        $main = explode($delim, $key);
        if (count($main) < 2)
            continue;
    
            $bottom = &$output;
            while(count($main) > 1) 
            {
                    $sub = array_shift($main);
                    if (!isset($bottom[$sub]))
                    {
                        $bottom[$sub]  =  array();
                    }
                        $bottom = &$bottom[$sub];
            }
                    $bottom[] = $main[count($main)-1];
        }
        return $output;
     }
    
        // Input
        print_r( $array);
    
        // Output
        print_r( build_arr($array) );
    
    
     ?>
    

    Output

     [akshay@localhost tmp]$ php test.php
     Array
     (
         [0] => dashboard
         [1] => dashboard_read
         [2] => dashboard_update
         [3] => dashboard_create
         [4] => dashboard_delete
         [5] => dashboard_search
         [6] => timeplan_event_read
         [7] => timeplan_event_search
         [8] => timeplan_read
         [9] => timeplan_search
         [10] => webhotel
         [11] => webhotel_read
         [12] => webhotel_update
         [13] => webhotel_create
         [14] => webhotel_delete
         [15] => webhotel_search
     )
    
     Array
     (
         [dashboard] => Array
             (
                 [0] => read
                 [1] => update
                 [2] => create
                 [3] => delete
                 [4] => search
             )
    
         [timeplan] => Array
             (
                 [event] => Array
                     (
                         [0] => read
                         [1] => search
                     )
    
                 [0] => read
                 [1] => search
             )
    
         [webhotel] => Array
             (
                 [0] => read
                 [1] => update
                 [2] => create
                 [3] => delete
                 [4] => search
             )
    
     )