1

This is my Array Output

<pre>Array
(
    [id] => 1

    [leadservice] => Array
        (

            [minutes] => 34

        )



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

    [leadservice] => Array
        (

            [minutes] => 34

        )



)
5
  • What you have tried so far? Commented Dec 18, 2019 at 10:46
  • You can do this by for loop Commented Dec 18, 2019 at 10:49
  • please give me coding solution Commented Dec 18, 2019 at 10:55
  • Its ok with for loop? Commented Dec 18, 2019 at 10:56
  • Check answer .. Commented Dec 18, 2019 at 11:09

2 Answers 2

3

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(
            ),
        ),
    ]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

You need to wrap all of your arrays into a single array $sitesArray before passing that to the collect function.
See detailed version added to answer. Note the addition of keyBy('site_id') which will give you an output of the site ID as the key and the summed minutes as the value.
keyBy('site_id') breaks it. Don't use that.
1

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'];
    }
  }
}

8 Comments

Your output will be in $arr
Array ( [1] => Array ( [site_id] => 1 [minutes] => ) [2] => Array ( [site_id] => 2 [minutes] => ) ).
i am not getting minutea only site id
why not getting minutes only site id?
You have mitutes value in $value['leadservice'] , right
|

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.