1

My result array is

array(
    (int) 0 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-07-16',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 1 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-07-16',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 2 => array(
        'parent_id' => '4',
        'student_name' => null,
        'date' => '2014-07-17',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 3 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-07-23',
        'total_hrs' => '02:00',
        'subject_price' => '400'
    ),
    (int) 4 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-07-23',
        'total_hrs' => '02:00',
        'subject_price' => '400'
    ),
    (int) 5 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-08-04',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 6 => array(
        'parent_id' => '4',
        'student_name' => null,
        'date' => '2014-08-04',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 7 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-08-11',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 8 => array(
        'parent_id' => '4',
        'student_name' => null,
        'date' => '2014-08-11',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 9 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-08-18',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 10 => array(
        'parent_id' => '4',
        'student_name' => null,
        'date' => '2014-08-18',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 11 => array(
        'parent_id' => '3',
        'student_name' => null,
        'date' => '2014-08-25',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    ),
    (int) 12 => array(
        'parent_id' => '4',
        'student_name' => null,
        'date' => '2014-08-25',
        'total_hrs' => '02:00',
        'subject_price' => '500'
    )
)

I want following result so that I will get the sum of "Subject Price" on the basis of parent id and month

array(
    '07-2014' => array(
        '3 (i.e Parent id)' => '1500 (i.e. sum of subject for parent 3 and month 07-2014)',
        '4 (i.e Parent id)' => '800 (i.e. sum of subject for parent 3 and month 07-2014)',
    ),
    '08-2014' => array(
        '3 (i.e Parent id)' => '2000 (i.e. sum of subject for parent 3 and month 07-2014)',
        '4 (i.e Parent id)' => '2000 (i.e. sum of subject for parent 3 and month 07-2014)',
    )
)

Following is the updated code

$sum = array();
    foreach ($array_push as $v) {
        if (!isset($sum[date('m-Y', strtotime($v['date']))])) {
            $sum[date('m-Y', strtotime($v['date']))] = array();
        }
        if (!isset($sum[date('m-Y', strtotime($v['date']))][$v['parent_id']])) {
            $sum[date('m-Y', strtotime($v['date']))][$v['parent_id']] = 0;
        }
        $sum[date('m-Y', strtotime($v['date']))][$v['subject_price']] += $v['subject_price'];
        $sum[date('m-Y', strtotime($v['date']))][$v['parent_id']]++;
    }

Result I am getting. array( '07-2014' => array( (int) 3 => (int) 4, (int) 500 => (int) 1500, (int) 4 => (int) 1, (int) 400 => (int) 800 ), '08-2014' => array( (int) 3 => (int) 4, (int) 500 => (int) 4000, (int) 4 => (int) 4 ) )

Is there any way that I can have key as parent_id and sum of subject_price as value for that particular month.

Can you please suggest how can it be done. Your help will be appreciated. Let me know if you need any more information.

Thanks in advance.

2 Answers 2

1

This should do the trick (given $array is you input array) :

<?php
$array= array(...);
$sum = array();
foreach($array as $v){
    $d = explode('-', $v['date']);
    $d = $d[1].'-'.$d[0];
    if(!isset($sum[$d])) {
        $sum[$d] = array();
    }
    if(!isset($sum[$d][$v['parent_id']])) {
        $sum[$d][$v['parent_id']] = 0;
    }

    $sum[$d][$v['parent_id']] += $v['subject_price'];

}

var_export($sum);

To have all your "i.e. something" added, you can add this after :

$printable = array();
foreach($sum as $d => $g) {
    $printable[$d] = array();
    foreach($g as $p => $v) {
        $printable[$d][sprintf('%s (i.e Parent id)', $p)] = sprintf('%d (i.e. sum of subject for parent %s and month %s)', $v, $p, $d)
    }
}

var_export($printable);
Sign up to request clarification or add additional context in comments.

3 Comments

Note that the indexes in output array should ignore day(s).
Thanks for your reply. I edited my code. see response I am getting.
I edited it once again, following what you have added in your question.
1

Try using a sql query like:

select attr1, attr2, sum(attr2) as sum from myTable group by attr1, attr2;

and then use it as wanted

1 Comment

Thanks for your comment. But, in my application it is not possible to do it using group by because I am seralizing the data so for that reason I am facing problem

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.