php codeigniter从foreach中选择结果并保存到数组中

时间:2013-07-10 21:07:47

标签: php codeigniter function foreach

我有一个php mysql查询结果如下......

Array
(
    [0] => stdClass Object
        (
            [id] => 52
            [sku] => 
            [name] => stone product 52
    )
    [1] => stdClass Object
        (
            [id] => 53
            [sku] => 
            [name] => stone product 53
    )

    .   
    .
    .   

)

Array
(
    [0] => stdClass Object
        (
            [id] => 12
            [sku] => 
            [name] => stone product 12
    )
    [1] => stdClass Object
        (
            [id] => 13
            [sku] => 
            [name] => stone product 13
    )

    .   
    .
    .   

)

我希望结果如下所示

Array(
    [0] => stdClass Object
        (
            [id] => 52
            [sku] => 
            [name] => stone product 52
    )
      [1] => stdClass Object
        (
            [id] => 53
            [sku] => 
            [name] => stone product 53
    )
     [2] => stdClass Object
        (
            [id] => 12
            [sku] => 
            [name] => stone product 12
    )
     [3] => stdClass Object
        (
            [id] => 13
            [sku] => 
            [name] => stone product 13
    )

在一个数组中

请建议我使用php代码

2 个答案:

答案 0 :(得分:0)

你不应该在php中加入这些结果。使用one query获取两组结果,这将减少数据库读取。

答案 1 :(得分:0)

您可以使用array_merge函数:http://php.net/manual/en/function.array-merge.php

如果你正在处理一个内部有数组的数组,你可以将它们与foreach调用合并

$my_array=get_the_result_from_mysql();//Here how do you get the result.

$new_array=array();
foreach ($my_array as $row) {

$new_array=array_merge($new_array,$row);

}

var_dump($new_array);

我认为这有助于你。