Codeigniter格式化数据库选择查询行

时间:2016-08-17 11:24:28

标签: php mysql codeigniter

我正在使用codeigniter。 mysql是数据库。 我有一个如下所示的查询,它完美无缺。

$this->db->select(
                "orders.book_id,products.product_id,
                 products.price,orders.user_id,
                 products.parent_id,orders.booked_date
                "
        );

        $this->db->from("orders");
        $this->db->join('order_details', 'orders.book_id = order_details.book_id');
        $this->db->join('products', 'order_details.product_id = products.product_id');

我需要格式化结果,我知道可以使用一些循环从PHP完成。但我必须使用mysql来提高性能。

我需要使用父键作为键来获取多维数组。 在另一组数组中,产品密钥为关键。 我希望得到的样本数组如下。有可能得到 没有循环结果的那种格式的响应? 最好使用mysql本身?

Array
(
    //5 is parent id
    [5] => Array (
          //10 is product id
          [10] => Array(

              [0] => stdClass Object
              (
                    [book_id] => 345
                    [product_id] => 10
                    [price] => 12
                    [user_id] => 0
                    [parent_id] => 5
                    [booked_date] => 02-08-2016
              )


          )

          [7] => Array(

              [0] => stdClass Object
              (
                 [book_id] => 343
                 [product_id] => 7
                 [price] => 12
                 [user_id] => 34
                 [parent_id] => 5
                 [booked_date] => 02-08-2016
              )


          )

    )


    [1] => Array (

          [3] => Array(

              (
                 [book_id] => 341
                 [product_id] => 3
                 [price] => 11
                 [user_id] => 34
                 [parent_id] => 1
                 [booked_date] => 02-08-2016
               )


          )

          [11] => Array(

              [0] => stdClass Object
              (
                 [book_id] => 343
                 [product_id] => 11
                 [price] => 12
                 [user_id] => 34
                 [parent_id] => 1
                 [booked_date] => 02-08-2016
              )

              [1] => stdClass Object
              (
                [book_id] => 335
                [product_id] => 11
                [price] => 11
                [user_id] => 34
                [parent_id] => 1
                [booked_date] => 02-08-2016
              )


          )

    )

)

0 个答案:

没有答案