Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert flat multidimensional array into a hierarchical multidimensional array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 119
    Answer it

    I am trying to list photo albums where each album contains individual photos. This is similar to categories where each category cotains posts.
    I have two database tables, namely "albums" and "photos". 
    The albums table has columns id, album_name, and album_description.
    The photos table has columns id, photo_is_in, photo_name and photo_description.

    I used INNER JOIN to combine  the albums and photos table to produce the flat $albums array below: 

    <?php
    
    $albums  = [
    ['album_id' => '1', 'photo_id' => '41', 'album_name' => 'album_1', 'album_description' => 'Album 1 description', 'photo_is_in' => 'album_1', 'photo_name' => 'photo_1.jpg', 'photo_description' => 'Photo 1 description'],
    
    ['album_id' => '1', 'photo_id' => '42', 'album_name' => 'album_1', 'album_description' => 'Album 1 description', 'photo_is_in' => 'album_1', 'photo_name' => 'photo_2.jpg', 'photo_description' => 'Photo 2 description'],
    
    //==================
    
    ['album_id' => '2', 'photo_id' => '43', 'album_name' => 'album_2', 'album_description' => 'Album 2 description', 'photo_is_in' => 'album_2', 'photo_name' => 'photo_3.jpg', 'photo_description' => 'Photo 3 description'],
    
    ['album_id' => '2', 'photo_id' => '44', 'album_name' => 'album_2', 'album_description' => 'Album 2 description', 'photo_is_in' => 'album_2', 'photo_name' => 'photo_4.jpg', 'photo_description' => 'Photo 4 description'],
    
    ];
    
    ?>
    
    //I want to turn the $albums array above into a hierachical array like below:
    
    <?php
    
    $sameAlbums = Array(
    
        'album_1' => Array( //comes from 'album_name' => 'album_1' in the $data array above
            'album_id' => '1',
            'album_description' => 'Album 1 description',
            'album_1' => Array( //comes from 'phot_is_in' => 'album_1' in the $data array above
                
                'photo_1.jpg' => Array( 
                    'photo_id' => '41',
                    'photo_description' => 'Photo 1 deescription',
                    
                ), 
            
                
                'photo_2.jpg' => Array( 
                    'photo_id' => '42',
                    'photo_description' => 'Photo 2 deescription',
                    
                ) 
                
                
            )
            
        ),
        
        //============================================
    
        'album_2' => Array( //comes from 'album_name' => 'album_2' in the $data array above
            'album_id' => '2',
            'album_description' => 'Album 2 description',
            'album_2' => Array( //comes from 'phot_is_in' => 'album_2' in the $data array above
                
                'photo_3.jpg' => Array( 
                    'photo_id' => '43',
                    'photo_description' => 'Photo 3 deescription',
                    
                ), 
            
                
                'photo_3.jpg' => Array( 
                    'photo_id' => '44',
                    'photo_description' => 'Photo 4 deescription',
                    
                ) 
                
                
            ) 
            
        ) 
        
    );
    
    ?>


    I have tried something like this which did not work as expected and I need help here:

    <?php
    $grouping = [];
    foreach($albums as $albumName){
        //Grouping data by album name
        $grouping[$albumName['phot_is_in']] = $albumName[]; 
    }
    
    echo "<pre>";
    echo print_r($grouping);
    
    ?>

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: