在PHP中从给定数组创建数组
我的结果是在PHP中从给定数组创建数组,php,arrays,Php,Arrays,我的结果是 array( (int) 0 => array( 'parent_id' => '3', 'student_name' => null, 'date' => '2014-07-16', 'total_hrs' => '02:00', 'subject_price' => '500' ), (int) 1 => array(
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'
)
)
我希望得到以下结果,以便根据家长id和月份得到“主题价格”的总和
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)',
)
)
以下是更新的代码
$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']]++;
}
结果我得到了。
排列(
'07-2014'=>阵列(
(int)3=>(int)4,
(int)500=>(int)1500,
(int)4=>(int)1,
(int)400=>(int)800
),
“08-2014”=>阵列(
(int)3=>(int)4,
(int)500=>(int)4000,
(int)4=>(int)4
)
)
有没有什么方法可以让我把密钥作为父id,把主题价格的总和作为那个月的值
你能建议一下怎么做吗。谢谢你的帮助。如果你需要更多的信息,请告诉我
提前感谢。这应该可以做到(假设$array是您的输入数组):
尝试使用sql查询,如:
select attr1, attr2, sum(attr2) as sum from myTable group by attr1, attr2;
然后按需要使用它注意,输出数组中的索引应该忽略天。感谢您的回复。我编辑了我的代码。请参阅我收到的回复。我根据您在问题中添加的内容再次编辑了它。非常感谢您这正是我想要的。谢谢您的评论。但是,在我的应用程序中,不可能使用group by来完成,因为我正在对数据进行序列化,因此我面临着这个问题
$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);
select attr1, attr2, sum(attr2) as sum from myTable group by attr1, attr2;