如何使用PHP

时间:2017-08-07 07:29:37

标签: php arrays yii array-merge

for ($row = 0; $row < 25; $row++) {
    $bQuery = 'Select date, billed from {$tableArray[$row][1]}';
    $bProvider = new CSqlDataProvider($bQuery, array('pagination'=>false));
    $Arr2 = $bProvider->getData();
    // merging array to get all loop records in single array
    $array_2 = array_merge($array_2, $Arr2);
}

然后print_r $array_2输出为:

Array
(
    [0] => Array
        (
            [date] => 2016-11-00
            [billed] => 100
        )
    [1] => Array
        (
            [date] => 2016-12-00
            [billed] => 400
        )
    [2] => Array
        (
            [date] => 2016-11-00
            [billed] => 200
        )
     // so on
)

我想将billeddate is same相加。所以这是我的代码,它适用于它。

$result = array();
foreach ($array_2 as $data) {
    @$result[$data['date']] += $data['billed'];
}

print_r($result);

$newresultGraph = array();
foreach ($result as $date => $billed) {
    $newresultGraph[] = array('date' => $date, 'billed' => $billed);
}

print_r($newresultGraph);

以上代码中的print_r($result)会返回如下所示的数据,它已经汇总了同一日期的值。

Array
(
    [2016-11-00] => 300
    [2016-12-00] => 400
)

然后在上面的代码中,在另一个foreach循环之后,我在数组中创建我之前的数据。当我print_r($newresultGraph)时,我的数据是这样的。

Array
(
    [0] => Array
        (
            [date] => 2016-11-00
            [billed] => 300
        )

    [1] => Array
        (
            [date] => 2016-12-00
            [billed] => 400
        )

)

这对我来说非常合适..因为我的查询就是这个。

Select date, billed from table'

我只从表中获得datebilled,但现在我有另一个字段,所以我的查询就像这样..

Select 
date, 
billed, 
((billed * rate) * (myValue) / 100) as my_rev,
((billed * rate) * (opValue) / 100) as op_rev,
From table

所以我有两个新值my_revop_rev

然后print_r $array_2输出为:

Array
(
    [0] => Array
        (
            [date] => 2016-11-00
            [billed] => 100
            [my_rev] => 30
            [op_rev] => 70
        )
    [1] => Array
        (
            [date] => 2016-12-00
            [billed] => 400
            [my_rev] => 150
            [op_rev] => 250
        )
    [2] => Array
        (
            [date] => 2016-11-00
            [billed] => 200
            [my_rev] => 50
            [op_rev] => 150
        )
     // so on
)

这是正确的,

我想将所有结算,my_rev,op_rev的值相加一个日期..我不想总计结算+ my_rev + op_rev ..

我希望我的数组如下所示:它应该将所有结算的值汇总到计费中,类似于my_rev和op_rev。

就像下面的例子一样。

Array
(
    [0] => Array
        (
            [date] => 2016-11-00
            [billed] => 300
            [my_rev] => 80
            [op_rev] => 220
        )
[1] => Array
    (
        [date] => 2016-12-00
        [billed] => 400
        [my_rev] => 150
        [op_rev] => 250
    )

)

我的上述代码正在将print_r($result)变成这样:

Array
(
    [2016-11-00] => 600
    [2016-12-00] => 800
)

我该怎么办?还有其他建议吗?

3 个答案:

答案 0 :(得分:1)

此代码执行此操作:

$tmp_array = array();
foreach ($array as $val) {
    if (array_key_exists($val['date'], $tmp_array)) {
        $tmp_array[$val['date']]['billed'] += $val['billed'];
        $tmp_array[$val['date']]['my_rev'] += $val['my_rev'];
        $tmp_array[$val['date']]['op_rev'] += $val['op_rev'];
    } else {
        $tmp_array[$val['date']]['date'] = $val['date'];
        $tmp_array[$val['date']]['billed'] = $val['billed'];
        $tmp_array[$val['date']]['my_rev'] = $val['my_rev'];
        $tmp_array[$val['date']]['op_rev'] = $val['op_rev'];
    }
}

$result = array_values($tmp_array);

输出:

Array
(
    [0] => Array
        (
            [date] => 2016-11-00
            [billed] => 300
            [my_rev] => 80
            [op_rev] => 220
        )
    [1] => Array
        (
            [date] => 2016-12-00
            [billed] => 400
            [my_rev] => 150
            [op_rev] => 250
        )
    )
)

$tmp_array作为日期作为键对数据进行分组,最后一个用连续索引替换这些键。如果您希望将日期保留为关键字 - 它会更优化 - 或者不关心,只需拨打$tmp_array $result并删除最后一行。

答案 1 :(得分:0)

只需像这样更改你的第二个foreach,然后打印结果数组,看它会用键和值进行求和。

$result = array();
foreach ($array_2 as $data) {
    @$result[$data['date']]['billed'] += $data['billed'];
    @$result[$data['date']]['my_rev'] += $data['my_rev'];
    @$result[$data['date']]['op_rev'] += $data['op_rev'];
}

如果需要,您可以相应地更改最后一个foreach。

结果显示您的值:

输入:

Array
(
[0] => Array
    (
        [date] => 2016-11-00
        [billed] => 100
        [my_rev] => 30
        [op_rev] => 70
    )

[1] => Array
    (
        [date] => 2016-12-00
        [billed] => 400
        [my_rev] => 150
        [op_rev] => 250
    )

[2] => Array
    (
        [date] => 2016-11-00
        [billed] => 200
        [my_rev] => 50
        [op_rev] => 150
    )

)

结果:

Array
(
[2016-11-00] => Array
    (
        [billed] => 300
        [my_rev] => 80
        [op_rev] => 220
    )

[2016-12-00] => Array
    (
        [billed] => 400
        [my_rev] => 150
        [op_rev] => 250
    )
)

答案 2 :(得分:0)

有两种选择:

  • 以PHP计算总和
  • 计算SQL中的总和

以PHP计算总和

$sql = <<<SQL
SELECT
    date, 
    billed, 
    billed * rate * myValue / 100 AS my_rev,
    billed * rate * opValue / 100 AS op_rev
FROM
    table
SQL;

$provider = new CSqlDataProvider($sql, array(
    'pagination' => false,
));

$rows = $provider->getData();

$data = array();

foreach ($rows as $row) {
    $date = $row['date'];

    // initialize
    if (!array_key_exists($date, $data)) {
        $data[$date] = array(
            'date' => $date,
            'billed' => $row['billed',
            'my_rev' => $row['my_rev'],
            'op_rev' => $row['op_rev',
        );

        continue;
    }

    $data[$date]['billed'] += $row['billed'];
    $data[$date]['my_rev'] += $row['my_rev'];
    $data[$date]['op_rev'] += $row['op_rev'];
}

// sort by key, that is, sort by date
ksort($data);

// remove keys
$data = array_values($data);

var_dump($data);

计算SQL

中的总和
$sql = <<<SQL
SELECT
    date, 
    SUM(billed), 
    SUM(billed * rate * myValue / 100) AS my_rev,
    SUM(billed * rate * opValue / 100) AS op_rev
FROM
    table
GROUP BY
    date
ORDER BY
    date ASC
SQL;

$provider = new CSqlDataProvider($sql, array(
    'pagination' => false,
));

$rows = $provider->getData();

var_dump($rows);

您自己决定选择哪种解决方案。但是,后者需要的PHP代码较少,毕竟数据库服务器也可以做一些工作,对吗?