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.
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 "ie 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);
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.