创建一个从mysqli查询结果中分成两部分的数组

时间:2018-04-24 23:20:05

标签: php

对于我想要实现的目标,我不确定正确的术语(多维?),因此我会向您展示我的代码,并尝试解释。 我在zen cart中工作,这就是数据库查询使用$ db-> execute。

的原因

以下是构建我的数组的函数

   $oID = (int)$oID;
   $combined =  $db->execute("SELECT op.orders_id, op.products_model,
                    op.products_name, op.products_quantity,
                    o.customers_name
                    FROM " . TABLE_ORDERS_PRODUCTS . " op
                    LEFT JOIN " . TABLE_ORDERS . " o
                    ON op.orders_id = o.orders_id
                    WHERE op.orders_id = $oID" . $this->product_filter);

    while (!$combined->EOF) {
        $order_id = $combined->fields['orders_id'];
        $customers_name = $combined->fields['customers_name'];
        $products_name = $combined->fields['products_name'];
        $products_model = $combined->fields['products_model'];
        $products_quantity = $combined->fields['products_quantity'];

        $this_order_data = array(
                               'order' => $order_id,
                               'customer' => $customers_name,
                               'name' => $products_name,
                               'model' => $products_model,
                               'quantity' => $products_quantity
                              );
    echo'<pre>';print_r($this_order_data);echo'</pre>';
    $combined->MoveNext();
    }

这给我的结果如

Array
(
[order] => 1913
[customer] => Customer A
[name] => Product 1
[model] => P1
[quantity] => 15
)
Array
(
[order] => 1913
[customer] => Customer A
[name] => Product 2
[model] => P2
[quantity] => 15
)
Array
(
[order] => 1912
[customer] => Customer B
[name] => Product 3
[model] => P3
[quantity] => 5
)
Array
(
[order] => 1911
[customer] => Customer C
[name] => Product 2
[model] => P2
[quantity] => 75
)
Array
(
[order] => 1911
[customer] => Customer C
[name] => Product 4
[model] => P4
[quantity] => 30
)
Array
(
[order] => 1910
[customer] => Customer D
[name] => Product 5
[model] => P5
[quantity] => 31
)

我真正想要的是让它更像这样

Array
(
[order] => 1911
[customer] => Customer C
) 
    Array
    (
    [name] => Product 2
    [model] => P2
    [quantity] => 75
    )
    Array
    (
    [name] => Product 4
    [model] => P4
    [quantity] => 30
    )
Array
(
[order] => 1910
[customer] => Customer D
)
    Array
    (
    [name] => Product 5
    [model] => P5
    [quantity] => 31
    )

基本上,我的部分SQL结果(订单和客户)形成一个数组,然后为该订单下的每个订单项重复名称,型号和数量。然后它继续下一个订单。

如何将我的sql结果拆分为两个数组,其中一个在另一个数组内?

编辑:最终目标是能够回显显示订单ID和客户名称的报告,然后是包含订单的产品列表。

1 个答案:

答案 0 :(得分:1)

如果orders_id是TABLE_ORDERS的主键,您可以像这样修改代码:

 $result = []; //somewhere early, before while, initialize empty array

以后:

 //replace from $this_order_data = array( ...
 $this_order_data = array(
                           'name' => $products_name,
                           'model' => $products_model,
                           'quantity' => $products_quantity
                         );
if (isset($result[$order_id])) {
  $result[$order_id]['products'][] = $this_order_data;
} else {
  $result[$order_id]['order_id'] = $order_id;
  $result[$order_id]['customer_name'] = $customer_name; //here we need the primary key
                                                    //exactly one customer per order
  $result[$order_id]['products'][] = $this_order_data;
}
$combined->MoveNext();
} //end while

var_dump(array_values($result)); //you can remove array_values, 
      //if you don't mind that the order ids are array keys.