对来自多个数据库查询的数据进行分组和计数以创建关联的二维数组

问题描述 投票:0回答:2

有人可以解释一下如何从下面的数组中获取这个结果吗? 这是我正在寻找的结果:

Array
(
    [Nov 18, 2011] => Array
        (
            [C] => 2
            [I] => 1
        )

    [Nov 22, 2011] => Array
        (
            [C] => 2
        )

)

这是包含原始数据的数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Nov 18, 2011] => C
                )

            [1] => Array
                (
                    [Nov 18, 2011] => C
                )

            [2] => Array
                (
                    [Nov 18, 2011] => I
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Nov 22, 2011] => C
                )

            [1] => Array
                (
                    [Nov 22, 2011] => C
                )

        )

)

显示的第一个数组表示内部数组每个元素的项目数,这是我试图从下一个数组中总结的内容。

这是生成上述数组的脚本:

$qrybilled = $this->db->query("SELECT tbltc.BILLED FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario GROUP BY BILLED");
$x = 0; $arr_billed = array();

foreach ($qrybilled->result() as $row) {
$qry3 = $this->db->query("SELECT tbltc.* FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario AND tbltc.BILLED = '".$row->BILLED."' GROUP BY TC ORDER BY CAT ASC, TC ASC");
    
    $tmp3 = array();
    foreach ($qry3->result() as $row) {     
        $tmp3[] = array( date("M d, Y",strtotime($row->BILLED)) => $row->CAT);
    }
    $arr_billed3[] = $tmp3; 

}
$data['billed3'] = $arr_billed3;
php arrays codeigniter grouping counting
2个回答
4
投票
$arr[0][] = array('Nov 18, 2011'=>'C');
$arr[0][] = array('Nov 18, 2011'=>'C');
$arr[0][] = array('Nov 18, 2011'=>'I');

$arr[1][] = array('Nov 22, 2011'=>'C');
$arr[1][] = array('Nov 22, 2011'=>'C');

$result = array();
foreach($arr as $key=>$value){
    foreach($value as $k=>$v){
        foreach($v as $_k=>$_v){
            if(isset($result[$_k][$_v])){
                $result[$_k][$_v]++;
            }
            else {
                $result[$_k][$_v] =1;
            }
        }
    }
}

数组结果:

print_r($result);

Array
(
    [Nov 18, 2011] => Array
        (
            [C] => 2
            [I] => 1
        )

    [Nov 22, 2011] => Array
        (
            [C] => 2
        )

)

0
投票

您不应该进行迭代查询来收集相关的数据库行,请在查询中使用 GROUP BY。 然后使用基本循环将数据折叠到所需的关联数组中。您的模型方法应如下所示。 (非 CodeIgniter PHPize 演示)

function getScenarioCountsByDate(int $pn, int $y, string $scenario): array
{
    $sql = <<<SQL
        SELECT 
            DATE_FORMAT(billed, '%b %d, %Y') billed_date,
            cat,
            COUNT(*) count
        FROM 
            tbltc
        WHERE 
            pn = ?
            AND y = ?
            AND scenario = ?
        GROUP BY 
            billed_date,
            cat
        ORDER BY 
            billed_date,
            cat
    SQL;

    $result = [];
    foreach ($this->db->query($sql, [$pn, $y, $scenario])->result() as $row) {
        $result[$row->billed_date][$row->cat] = $row->count;
    }
    return $result;
}

从模型方法返回到控制器方法的有效负载:

[
  'Nov 18, 2011' => [
    'C' => 2,
    'I' => 1,
  ],
  'Nov 22, 2011' => [
    'C' => 2,
  ],
]
© www.soinside.com 2019 - 2024. All rights reserved.