开发者

Filtering a PHP array containing dates into a yearly summary

I'm looking at a way to create a summary of transactions within a certain month based on the contents of a PHP array.

Intended outcome (excusing layout):

-------------------------------------------
|   December 2009    |         12         |
|   January  2010    |         02         |
|   February 2010    |         47         |
|   March    2010    |         108        |
|   April    2010    |         499        |
-------------------------------------------

Based on my array:

Array
(
    [0] => Array
        (
            [name] => 2009-10-23
            [values] => Array
                (
                    [0] => INzY2MTI4ZWM4OGRm
                )

        )

    [1] => Array
        (
            [name] => 2009-10-26
            [values] => Array
                (
                    [0] =&开发者_如何转开发gt; IYmIzOWNmMmU3OWQz
                )

        )

    [2] => Array
        (
            [name] => 2009-11-23
            [values] => Array
                (
                    [0] => INTg4YzgxYWU1ODkx
                    [1] => IMjhkNDZkY2FjNDhl
                )

        )

    [3] => Array
        (
            [name] => 2009-11-24
            [values] => Array
                (
                    [0] => INTg4YzgxYWU1ODkx
                    [1] => INTg4YzgxYWU1ODkx
                )

        )

    [4] => Array
        (
            [name] => 2009-12-01
            [values] => Array
                (
                    [0] => IMWFiODk5ZjU1OTFk
                )

        )

I've had absolutely no luck no matter what I've tried. Especially with adding months that do not contain any variables.


This is a typical case for reducing the data array into the summary that you want.

$data = array(
    array('name' => '2009-10-23', 'values' => array('INzY2MTI4ZWM4OGRm')),
    array('name' => '2009-10-26', 'values' => array('IYmIzOWNmMmU3OWQz')),
    array('name' => '2009-11-23', 'values' => array('INTg4YzgxYWU1ODkx', 'IMjhkNDZkY2FjNDhl')),
    array('name' => '2009-11-24', 'values' => array('INTg4YzgxYWU1ODkx', 'INTg4YzgxYWU1ODkx')),
    array('name' => '2009-12-01', 'values' => array('IMWFiODk5ZjU1OTFk'))
);

function count_by_month($reduced, $current) {
    $month = date('F Y', strtotime($current['name']));

    if ( ! isset($reduced[$month])) {
        $reduced[$month] = 0;
    }

    $reduced[$month] += count($current['values']);

    return $reduced;
}

// Take the data array and return an array of
// month (key) and transaction count for that month (value)
$counts = array_reduce($data, 'count_by_month');

print_r($counts);


$summary = array();
foreach($transactions as $t)
{
    $month = substr($t['name'], 0, 7);
    if(!isset($summary[$month]))
        $summary[$month] = 0;

    if(isset($t['values']))
        $summary[$month] += count($t['values']);
}

will result in:

Array
(
    [2009-10] => 2
    [2009-11] => 4
    [2009-12] => 1
)


If I understand well:

  1. initialize an array $A with 12 elements, setting each element to 0
  2. loop over your array and for each value $v compute the month $m of $v['name'], compute the number of element in $v['values'] and add that number of element to $A[$m]
  3. print the content of $A with a nice formatting

If that's not what you need, perhaps you need to give an example where expected output matches your array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜