填充多维数组

时间:2011-11-26 23:21:23

标签: php arrays codeigniter multidimensional-array

我正在尝试使用CodeIgniter填充多维数组。在下面的代码中,外部foreach循环2次。内部的foreach循环3次,然后循环2次。我遇到的问题是我得到的$ data ['tc']包含一个包含5个项目的数组,但我想最终得到2个包含2个和3个项目的数组。

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

foreach ($bd->result() as $bdrow) {
    $tcbd = $this->db->query("SELECT tbltc.BILLED, tbltc.TC, tbltc.CAT FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario AND tbltc.BILLED = '".$bdrow->BILLED."' GROUP BY TC");

        foreach ($tcbd->result() as $row) {
            $tmp[] = array( $row->BILLED => $row->TC);
            }

    $data['tc'] = $tmp; 
}

目前我收到的数组如下:

Array
(
    [0] => Array
        (
            [2011-11-18 00:00:00] => C
        )

    [1] => Array
        (
            [2011-11-18 00:00:00] => I
        )

    [2] => Array
        (
            [2011-11-18 00:00:00] => S
        )

    [3] => Array
        (
            [2011-11-22 00:00:00] => C
        )

    [4] => Array
        (
            [2011-11-22 00:00:00] => S
        )

)

但是我需要把它分成两个独立的阵列,一个用于2011-11-18的日期,一个用于2011-11-22的日期。感谢。

3 个答案:

答案 0 :(得分:2)

您的代码中确实存在许多问题。除非我遗漏了什么,否则你的查询是多余的:

"SELECT tbltc.BILLED FROM tbltc 
WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario 
GROUP BY BILLED"

"SELECT tbltc.BILLED, tbltc.TC, tbltc.CAT FROM tbltc 
WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario 
AND tbltc.BILLED = '".$bdrow->BILLED."' 
GROUP BY TC"

您正在基于相同的$ pn,$ taxyear和$ scenario从同一个表中选择数据。一旦获得第一个结果集,就可以遍历它,只是为了将BILLED传递给第二个查询。这是完全没必要的,可以通过删除$ bdrow-> BILLED

单独通过第二个查询来完成
"SELECT tbltc.BILLED, tbltc.TC, tbltc.CAT FROM tbltc 
WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario 
GROUP BY TC"

其次,在两个查询中都有GROUP BY,但没有任何count(),sum()或其他聚合函数。使用GROUP BY尝试实现的目标是什么?所以,现在让我们为了锻炼而把它拿出来。您现在有一个带有笛卡尔结果的查询。这是代码

$tcbd = $this->db->query("SELECT tbltc.BILLED, tbltc.TC, tbltc.CAT FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario");
$data = array();
foreach ($tcbd->result() as $row) {
    $data[$row->BILLED][] = $row->TC;
}

会给你

Array
(
    [2011-11-18 00:00:00] => Array
        (
             [0] => C,
             [1] => I,
             [2] => S
        ),
    [2011-11-22 00:00:00] => Array
        (
             [0] => C,
             [1] => S
        )
)

关键位置可能会有所不同 - 我只是根据您的数据输入结果

答案 1 :(得分:0)

$tmp = array();
$dataarray = array();

foreach ($tcbd->result() as $row) {
    $dataarray['BILLED'] = $row->BILLED;
    $dataarray['TC'] = $row->TC;

    $tmp[] = $dataarray;
}

将创建:

Array
(
    [0] => Array
        (
            ['BILLED'] => 2011-11-18 00:00:00
            ['TC'] => C
        )

    [1] => Array
        (
            ['BILLED'] => 2011-11-18 00:00:00
            ['TC'] => I
        )

    [2] => Array
        (
            ['BILLED'] => 2011-11-18 00:00:00
            ['TC'] => S
        )

    [3] => Array
        (
            ['BILLED'] => 2011-11-18 00:00:00
            ['TC'] => C
        )

    [4] => Array
        (
            ['BILLED'] => 2011-11-18 00:00:00
            ['TC'] => S
        )
)

答案 2 :(得分:0)

之前:

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

foreach ($bd->result() as $bdrow) {
    $tcbd = $this->db->query("SELECT tbltc.BILLED, tbltc.TC, tbltc.CAT FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario AND tbltc.BILLED = '".$bdrow->BILLED."' GROUP BY TC");

    foreach ($tcbd->result() as $row) {
        $tmp[] = array( $row->BILLED => $row->TC);
    }

    $data['tc'] = $tmp; 
}
  • $ tmp永不重置
  • 每次循环时,
  • $ data ['tc']将始终覆盖
  • 应初始化您的变量

后:

$bd = $this->db->query("SELECT tbltc.BILLED FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y =         $taxyear AND tbltc.SCENARIO = $scenario GROUP BY BILLED");
$data = array();
foreach ($bd->result() as $bdrow) {
    $tcbd = $this->db->query("SELECT tbltc.BILLED, tbltc.TC, tbltc.CAT FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario AND tbltc.BILLED = '".$bdrow->BILLED."' GROUP BY TC");
    $tmp = array();
    foreach ($tcbd->result() as $row) {
        $tmp[] = array( $row->BILLED => $row->TC);
    }
    $data[] = $tmp; 
}

这应输出如下内容:

Array()
    [0] => Array() 
          [0] => X
          [1] => Y
    [1] => Array()
          [0] => Z
          ...
    ...