Reputation: 3615
I am trying to populate a multidimensional array with CodeIgniter. In the code below, the outer foreach loops 2 times. The inner foreach loops 3 times and then 2 times. The problem I am having is that my resulting $data['tc'] contains a single array containing 5 items, but I want to end up with 2 arrays containing 2 and 3 items respectively.
$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;
}
Currently I am receiving an array that looks like this:
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
)
)
But I need this to be broken into two separate arrays, one for the 2011-11-18 date, and one for the 2011-11-22 date. Thanks.
Upvotes: 1
Views: 9128
Reputation: 2141
You actually have a bunch of issues in your code. Unless I am missing something, your queries are redundant:
"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"
You are selecting data from the same table based on same $pn, $taxyear, and $scenario. Once you get the first result set you loop through it just to pass BILLED into the second query. That's completely unnecessary and can be accomplished with 2nd query alone by removing $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"
Second, you have GROUP BY in both queries but don't have any count(), sum(), or other aggregate functions. What are trying to achieve by using GROUP BY? So, let's take it out now for the sake of exercise. You now have one query with a cartesian result. Here's the code
$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;
}
will give you
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
)
)
key positions might vary - I simply typed the results based on your data
Upvotes: 2
Reputation: 1786
Before :
$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;
}
After:
$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;
}
This should output something like this :
Array()
[0] => Array()
[0] => X
[1] => Y
[1] => Array()
[0] => Z
...
...
Upvotes: 0
Reputation: 4293
$tmp = array();
$dataarray = array();
foreach ($tcbd->result() as $row) {
$dataarray['BILLED'] = $row->BILLED;
$dataarray['TC'] = $row->TC;
$tmp[] = $dataarray;
}
that will create:
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
)
)
Upvotes: 0