1

I have the below array

    Array
    (
        [0] => Array
            (
                [group_description] => Group 1
                [services] => Array
                    (
                        [0] => Array
                            (
                                [service_id] => 1
                                [service_name] => Security
                                [service_total] => 10
                            )

                        [1] => Array
                            (
                                [service_id] => 4
                                [service_name] => Catering
                                [service_total] => 20
                            )
                    )
            )
        [1] => Array
            (
                [group_description] => Group 2
                [services] => Array
                    (
                        [0] => Array
                            (
                                [service_id] => 1
                                [service_name] => Security
                                [service_total] => 6
                            )

                        [1] => Array
                            (
                                [service_id] => 4
                                [service_name] => Catering
                                [service_total] => 15
                            )
                        [2] => Array
                            (
                                [service_id] => 5
                                [service_name] => Lighting
                                [service_total] => 8
                            )
                    )
            )
    )

I want to sum arrays that have the same service and create a total array and put in back in to array like so

    Array
    (
        [0] => Array
            (
                [group_description] => Group 1
                [services] => Array
                    (
                        [0] => Array
                            (
                                [service_id] => 1
                                [service_name] => Security
                                [service_total] => 10
                            )

                        [1] => Array
                            (
                                [service_id] => 4
                                [service_name] => Catering
                                [service_total] => 20
                            )
                    )
            )
        [1] => Array
            (
                [group_description] => Group 2
                [services] => Array
                    (
                        [0] => Array
                            (
                                [service_id] => 1
                                [service_name] => Security
                                [service_total] => 6
                            )

                        [1] => Array
                            (
                                [service_id] => 4
                                [service_name] => Catering
                                [service_total] => 15
                            )
                        [2] => Array
                            (
                                [service_id] => 5
                                [service_name] => Lighting
                                [service_total] => 8
                            )
                    )
            )

        [3] => Array
            (
                [group_description] => Total
                [services] => Array
                    (
                        [0] => Array
                            (
                                [service_id] => 1
                                [service_name] => Security
                                [service_total] => 16
                            )

                        [1] => Array
                            (
                                [service_id] => 4
                                [service_name] => Catering
                                [service_total] => 35
                            )
                        [2] => Array
                            (
                                [service_id] => 5
                                [service_name] => Lighting
                                [service_total] => 8
                            )
                    )
            )
    )

I have tried looping the array and creating a new array while loooping. I used a foreach loop then used the array_search function to see which I am busy with and append to new array. but it does not achieve what i want,I always end up with the same array. Like I am literally caught in a loop. I have seen many examples but i think my case is not similar to examples

     foreach ($array as $value) { 

       if(array_search($value['service_id], array_column($array, 'service_id')) !== false) {
                    $newArray['service_id'] = $value['service_id'];
                    $newArray['service_name'] = $value['service_name'];
                    $newArray['service_total'] = $value['service_total'] + $newArray['service_total'];

                } else{ 
                    $newArray[] = $value; 
                }  
            } 

1 Answer 1

1

You need more than 1 foreach. Every deepest iteration you check if service is added. If yes, you add the current value, if not you add whole service. At the end you join to the current array.

function count_services($array)
{
    $service_list = [];
    foreach ($array as $row) {
        foreach ($row['services'] as $service) {
            if (isset($service_list[$service['service_id']])) {
                $service_list[$service['service_id']]['service_total'] += $service['service_total'];
            } else {
                $service_list[$service['service_id']] = $service;
            }
        }
    }

    return [
        'group_description' => 'Total',
        'services' => array_values($service_list)];
}

print_r(count_services($array));

Output:

Array
(
    [group_description] => Total
    [services] => Array
        (
            [0] => Array
                (
                    [service_id] => 1
                    [service_name] => Security
                    [service_total] => 16
                )

            [1] => Array
                (
                    [service_id] => 4
                    [service_name] => Catering
                    [service_total] => 35
                )

            [2] => Array
                (
                    [service_id] => 5
                    [service_name] => Lighting
                    [service_total] => 8
                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.