Shubham Azad
Shubham Azad

Reputation: 796

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

Upvotes: 1

Views: 133

Answers (3)

mickmackusa
mickmackusa

Reputation: 47883

Use a single loop to group data in the result array with temporary first level keys. Per group, save the two desired associative elements as parent values, then push whole rows into the data subarray. If you wish, after the loop, reindex the result by calling array_values(). Demo

$result = [];
foreach ($array as $row) {
    extract($row);
    $result[$video_category] ??= [
        'ctg_name' => $video_category_name,
        'cat_id' => $video_category,
    ];
    $result[$video_category]['data'][] = $row;
}
var_export(array_values($result));

Upvotes: 0

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7161

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
                        )

                )

        )

)

Upvotes: 2

Kirtee
Kirtee

Reputation: 141

You can try this. after your foreach loop.

foreach($video as $val){
    $result[$val['video_category']][] = $val;
}
$final_arr = [];
foreach($result as $val){
    $final_arr[] = [
                'ctg_name'=>$val[0]['video_category_name'],
                'cat_id'=>$val[0]['video_category'],
                'data'=>$val
                ]
}

Upvotes: 1

Related Questions