在PHP中从给定数组创建数组

时间:2014-07-21 09:58:32

标签: 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(
        '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']]++;
    }

结果我得到了。 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 ) )

有没有什么方法可以将key作为parent_id和subject_price的总和作为该特定月份的值。

你能否建议怎样才能做到。我们将不胜感激。如果您需要更多信息,请与我们联系。

提前致谢。

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题(给定$ 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);

拥有所有的#34;即。东西&#34;补充一下,你可以在以下后添加:

$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);

答案 1 :(得分:1)

尝试使用如下的SQL查询:

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

然后按需使用