PHP / CI将SQL查询传递给数组

时间:2012-06-20 22:19:01

标签: php sql arrays codeigniter

我在SQL查询中有一个组列表,希望将它们输出到一个数组,以便以后可以引用它。

目前我有以下代码循环播放这些组并将它们放入$test_array

$test_array = array();
$query = $this->site_model->list_groups();
foreach($query as $r1) :
    $test_array[] = $r1->group_name;
endforeach;

这显示以下数组:

Array
(
    [0] => Group 1
    [1] => Group 2
    [2] => Group 3
    [3] => Group 4
    [4] => Group 5
    [5] => Group 6
    [6] => Group 7
    [7] => Group 8
)

我希望将同一个查询传递给下面的数组,以便稍后在数组中添加其他段:

Array
(
    [Round 1] => Array
    (
        [0] => Group 1
        [1] => Group 2
        [2] => Group 3
        [3] => Group 4
        [4] => Group 5
        [5] => Group 6
        [6] => Group 7
        [7] => Group 8
    )
)

这可能吗?

1 个答案:

答案 0 :(得分:2)

$test_array = array('Round 1' => array());
...
$test_array['Round 1'][] = $r1->group_name;