phparraysmultidimensional-arraygroupingsub-array

Group 2d array data by a column and create new parent columns and subarrays for each group


I am trying to restructure a 2-level array data into a 3-level array by grouping on a column (video_category). Per each group, two columns should exist on the 2nd level (assigned new keys), to identify the group, and all original columns should be pushed as child rows of the parent group (in the 3rd level).

Input:

$array = [
    [
        'video_id' => 14,
        'video_title' => 'test1',
        'video_category_name' => 'Beginner',
        'video_category' => 1
    ],
    [
        'video_id' => 18,
        'video_title' => 'test',
        'video_category' => 1,
        'video_category_name' => 'Beginner'
    ],
    [
        'video_id' => 17,
        'video_title' => 'first',
        'video_category' => 3,
        'video_category_name' => 'Mobility'
    ],
    [
        'video_id' => 19,
        'video_title' => 'second',
        'video_category' => 3,
        'video_category_name' => 'Mobility'
    ]
];

Desired result:

[
    [
        'ctg_name' => 'Beginner',
        'cat_id' => 1,
        'data' => [
            [
                'video_id' => 14,
                'video_title' => 'test1',
                'video_category_name' => 'Beginner',
                'video_category' => 1,
            ],
            [
                'video_id' => 18,
                'video_title' => 'test',
                'video_category' => 1,
                'video_category_name' => 'Beginner',
            ],
        ],
    ],
    [
        'ctg_name' => 'Mobility',
        'cat_id' => 3,
        'data' => [
            [
                'video_id' => 17,
                'video_title' => 'first',
                'video_category' => 3,
                'video_category_name' => 'Mobility',
            ],
            [
                'video_id' => 19,
                'video_title' => 'second',
                'video_category' => 3,
                'video_category_name' => 'Mobility',
            ],
        ],
    ]
]

I'd tried the following, but it is not creating the parent level columns as desired.

foreach ($video as $val) {
    $result[$val['video_category']][] = $val;
}

Solution

  • try this code

    <?php
    $arr = array(
        0 => array
        (
            'video_id' => 14,
            'video_title' => 'test1',
            'video_category_name' => 'Beginner',
            'video_category' => 1
        ),
        1 => array
        (
            'video_id' => 18,
            'video_title' => 'test',
            'video_category' => 1,
            'video_category_name' => 'Beginner'
        ),
        2 => array
        (
            'video_id' => 17,
            'video_title' => 'first',
            'video_category' => 3,
            'video_category_name' => 'Mobility'
        ),
        3 => array
        (
            'video_id' => 19,
            'video_title' => 'second',
            'video_category' => 3,
            'video_category_name' => 'Mobility'
        )
    );
    echo "<pre>";
    $newArr = array();
    foreach($arr as $k=>$v){
        $newArr[$v['video_category_name']]['ctg_name']=$v['video_category_name'];
        $newArr[$v['video_category_name']]['cat_id']=$v['video_category'];
        $newArr[$v['video_category_name']]['data'][]=$v;
    }
    $newArr1 = array();
    foreach($newArr as $k=>$v){
       $newArr1[] = $v;
    }
    print_r($newArr);
    ?>
    

    Output

    Array
    (
        [Beginner] => Array
            (
                [ctg_name] => Beginner
                [cat_id] => 1
                [data] => Array
                    (
                        [0] => Array
                            (
                                [video_id] => 14
                                [video_title] => test1
                                [video_category_name] => Beginner
                                [video_category] => 1
                            )
    
                        [1] => Array
                            (
                                [video_id] => 18
                                [video_title] => test
                                [video_category] => 1
                                [video_category_name] => Beginner
                            )
    
                    )
    
            )
    
        [Mobility] => Array
            (
                [ctg_name] => Mobility
                [cat_id] => 3
                [data] => Array
                    (
                        [0] => Array
                            (
                                [video_id] => 17
                                [video_title] => first
                                [video_category] => 3
                                [video_category_name] => Mobility
                            )
    
                        [1] => Array
                            (
                                [video_id] => 19
                                [video_title] => second
                                [video_category] => 3
                                [video_category_name] => Mobility
                            )
    
                    )
    
            )
    
    )