VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

How to sum in multidimentional array?

This is my Array Output

<pre>Array
(
    [id] => 1

    [leadservice] => Array
        (

            [minutes] => 34

        )



)
<pre>Array
(
    [id] => 2

    [leadservice] => Array
        (

            [minutes] => 34

        )



)

Upvotes: 1

Views: 79

Answers (2)

Jon White
Jon White

Reputation: 1010

Your question is tagged with Laravel so if you turn your array into a Laravel collection you can start by grouping your items by their site_id then summing the minutes items in the leadservice items.

Something like:

collect($sitesArray)->groupBy('site_id')->map(function($site) {
    return $site->map(function($siteData) {
        return data_get($siteData, 'leadservice.minutes');
    })->sum();
});

You can also add a third parameter to the data_get() to default any missing minutes data, presumably you'd want to default to 0

* ADD DETAILED EXAMPLE *

public function test()
{
    return $this->setData()->groupBy('site_id')->map(function($site) {
            return $site->map(function($siteData, $key) {
                return data_get($siteData, 'leadservice.minutes');
            })->sum();
        });
}

public function setData()
{
    return collect([
        array(
            'id' => 1,
            'customer_lead_service_id' => 1,
            'customer_id' => 94,
            'site_id' => 1,
            'branch_id' => null,
            'start_date' => '2019-10-24',
            'end_date' => '2019-10-24',
            'price' => 24.00,
            'status' => 'completed',
            'confirmation_send_date' => null,
            'confirmation_date' => null,
            'assign_date' => '2019-11-21',
            'assign_to' => 94,
            'assign_by' => null,
            'assign_note' => null,
            'customer_remark' => null,
            'reopen_date' => null,
            'reopen_by' => null,
            'reopen_note' => null,
            'total_time' => 2,
            'leadservice' => array(
                'id' => 1,
                'customer_lead_id' => 2,
                'branch_id' => 10,
                'service_id' => 9,
                'service_title' => 'Sentinel Outlet Temperature Monitoring',
                'minutes' => 34,
                'quantity' => 1,
                'type' => 'quarterly',
                'contract_start_date' => null,
            ),
            'documents' => array(
                '0' => array(
                    'id' => 54,
                    'engineer_id' => 94,
                    'contract_id' => 1,
                    'original_name' => null,
                    'document' => '1574832562.mp4',
                    'document_type' => null,
                ),
            ),
            'notes' => array(
                    '0' => array(
                            'id' => 28,
                            'customer_contract_id' => 1,
                            'engineer_id' => 94,
                            'note' => 'note',
                        ),
                ),
        ),
    array(
        'id' => 2,
        'customer_lead_service_id' => 2,
        'customer_id' => 95,
        'site_id' => 1,
        'branch_id' => null,
        'start_date' => '2019-10-24',
        'end_date' => '2019-10-24',
        'price' => 700.00,
        'status' => 'running',
        'confirmation_send_date' => null,
        'confirmation_date' => null,
        'assign_date' => '2019-11-21',
        'assign_to' => 94,
        'assign_by' => null,
        'assign_note' => null,
        'customer_remark' => null,
        'reopen_date' => null,
        'reopen_by' => null,
        'reopen_note' => null,
        'total_time' => null,
        'leadservice' => array(
                'id' => 2,
                'customer_lead_id' => 3,
                'branch_id' => 10,
                'service_id' => 10,
                'service_title' => 'HWS Vessel Temperature Monitoring  Inspection',
                'minutes' => 12,
                'quantity' => 1,
                'type' => 'monthly',
                'contract_start_date' => null,
            ),
        'documents' => array(
            ),
        'notes' => array(
            ),
        ),
    array(
        'id' => 3,
        'customer_lead_service_id' => 3,
        'customer_id' => 96,
        'site_id' => 1,
        'branch_id' => null,
        'start_date' => '2019-10-26',
        'end_date' => '2019-10-24',
        'price' => 550.00,
        'status' => 'new',
        'confirmation_send_date' => null,
        'confirmation_date' => null,
        'assign_date' => '2019-11-21',
        'assign_to' => 94,
        'assign_by' => null,
        'assign_note' => null,
        'customer_remark' => null,
        'reopen_date' => null,
        'reopen_by' => null,
        'reopen_note' => null,
        'total_time' => null,
        'leadservice' => array(
                'id' => 3,
                'customer_lead_id' => 2,
                'branch_id' => 10,
                'service_id' => 11,
                'service_title' => 'Hot Water Heater Temperature Monitoring',
                'minutes' => 34,
                'quantity' => 1,
                'type' => 'six_monthly',
                'contract_start_date' => null,
            ),
        'documents' => array(
            ),
        'notes' => array(
            ),
        ),
    array(
        'id' => 4,
        'customer_lead_service_id' => 4,
        'customer_id' => 100,
        'site_id' => 2,
        'branch_id' => null,
        'start_date' => '2019-10-27',
        'end_date' => '2019-10-24',
        'price' => 24.00,
        'status' => 'new',
        'confirmation_send_date' => null,
        'confirmation_date' => null,
        'assign_date' => '2019-11-21',
        'assign_to' => 94,
        'assign_by' => null,
        'assign_note' => null,
        'customer_remark' => null,
        'reopen_date' => null,
        'reopen_by' => null,
        'reopen_note' => null,
        'total_time' => null,
        'leadservice' => array(
                'id' => 4,
                'customer_lead_id' => 3,
                'branch_id' => 10,
                'service_id' => 12,
                'service_title' => 'Tank Lid Installation',
                'minutes' => 12,
                'quantity' => 1,
                'type' => 'annually',
                'contract_start_date' => null,
            ),
        'documents' => array(
            ),
        'notes' => array(
            ),
        ),
    array(
        'id' => 5,
        'customer_lead_service_id' => 5,
        'customer_id' => 102,
        'site_id' => 2,
        'branch_id' => null,
        'start_date' => '2019-10-24',
        'end_date' => '2019-10-24',
        'price' => 45.00,
        'status' => 'new',
        'confirmation_send_date' => null,
        'confirmation_date' => null,
        'assign_date' => '2019-11-21',
        'assign_to' => 94,
        'assign_by' => null,
        'assign_note' => null,
        'customer_remark' => null,
        'reopen_date' => null,
        'reopen_by' => null,
        'reopen_note' => null,
        'total_time' => null,
        'leadservice' => array(
                'id' => 5,
                'customer_lead_id' => 3,
                'branch_id' => 10,
                'service_id' => 13,
                'service_title' => 'Fitting of Tank Insulation',
                'minutes' => 14,
                'quantity' => 1,
                'type' => 'annually',
                'contract_start_date' => null,
            ),
        'documents' => array(
            ),
        'notes' => array(
            ),
        ),
    ]);
}

Upvotes: 3

Yasin Patel
Yasin Patel

Reputation: 5731

Considering your array name is $data, try below code

$arr = [];
foreach ($data as $value) {
  foreach ($value['leadservice'] as $leadservice) {
    if(isset($arr[$value['site_id']]['minutes']) && $arr[$value['site_id']]['minutes']!=null){
      $arr[$value['site_id']]['minutes'] += $leadservice['minutes'];
    }
    else {
      $arr[$value['site_id']]['site_id'] = $value['site_id'];
      $arr[$value['site_id']]['minutes'] = $leadservice['minutes'];
    }
  }
}

Upvotes: 1

Related Questions