RJParikh
RJParikh

Reputation: 4166

count same categories from array and Store in new array with its count and category name

I have one array which I am getting from database query response.

Now I want to count same categories and print in option_name array. I have tried it with different array functions but want get desire output.

I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.

Array
(
    [0] => Array
        (
            [CNC] => Array
                (
                    [id] => 5
                    [category_id] => 68
                )

            [GVO] => Array
                (
                    [option_name] => Actors
                )

        )

    [1] => Array
        (
            [CNC] => Array
                (
                    [id] => 11
                    [category_id] => 72
                )

            [GVO] => Array
                (
                    [option_name] => Cricketers
                )

        )

    [2] => Array
        (
            [CNC] => Array
                (
                    [id] => 3
                    [category_id] => 72
                )

            [GVO] => Array
                (
                    [option_name] => Cricketers
                )

        )

    [3] => Array
        (
            [CNC] => Array
                (
                    [id] => 4
                    [category_id] => 74
                )

            [GVO] => Array
                (
                    [option_name] => Musician 
                )

        )

    [4] => Array
        (
            [CNC] => Array
                (
                    [id] => 7
                    [category_id] => 76
                )

            [GVO] => Array
                (
                    [option_name] => Directors
                )

        )

    [5] => Array
        (
            [CNC] => Array
                (
                    [id] => 6
                    [category_id] => 76
                )

            [GVO] => Array
                (
                    [option_name] => Directors
                )

        )
)

Desire Output:

Array
(
    [Actors] => 1
    [Cricketers] => 2
    [Musician] => 1
    [Directors] => 2
)

Thanks in advance!

Upvotes: 0

Views: 313

Answers (3)

Murad Hasan
Murad Hasan

Reputation: 9583

Try this:

$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
    if(in_array($value, $new_array)){
        $new_array[$value] = $new_array[$value]+1;
    }else{
        $new_array[$value] = 1;
    }
}

Output

Array
(
    [Actors] => 1
    [Cricketers] => 2
    [Musician] => 1
    [Directors] => 2
)

Upvotes: 1

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

You simply need to loop through the array using foreach like as

$result = [];
foreach($arr as $k => $v){
    if(isset($result[$v['GVO']['option_name']])){
        $result[$v['GVO']['option_name']] += 1;
    }else{
        $result[$v['GVO']['option_name']] = 1;
    }
}

print_R($result);

Upvotes: 1

FuzzyTree
FuzzyTree

Reputation: 32402

You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:

$counts = [];

foreach($array as $v) {
    if(!isset($counts[$v['GVO']['option_name']])) {
        $counts[$v['GVO']['option_name']] = 0;
    }

    $counts[$v['GVO']['option_name']]++;
}

print_r($counts);

Upvotes: 1

Related Questions