Search code examples
phparrayscodeigniter

PHP create array group by key and value


I have an array in PHP code below, and I want to convert this array to be grouped by data value. It's always hard to simplify arrays

     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
        )
)

And I expect this array :

(
    [0] => 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
                    )
                )   

    )
     [1] => 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
                    )
                ) 
    )
)

I'd tried this, but not getting my expected result.

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

It returns like this :

     Array
(
    [1] => 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
                    )
        )  

    [3] => 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
                )


    )
)    

Does anyone have an efficient way of doing this?. Please help me Thanks

     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
        )
)

And I expect this array :

(
    [0] => 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
                    )
                )   

    )
     [1] => 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
                    )
                ) 
    )
)

I'd tried this, but not getting my expected result.

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

It returns like this :

     Array
(
    [1] => 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
                    )
        )  

    [3] => 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
                )


    )
)    

Does anyone have an efficient way of doing this?. Please help me Thanks


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
                            )
    
                    )
    
            )
    
    )