我有一个从mysql查询生成的数组,如下所示。它们的关键是order_id
值。
订购数组
Array
(
[2646] => Array
(
[order_id] => 2646
)
[2647] => Array
(
[order_id] => 2647
)
[2650] => Array
(
[order_id] => 2650
)
[2658] => Array
(
[order_id] => 2658
)
)
我正在寻找使用来自另一个数组的项目向数组的每一行添加项目。
order_items数组
Array
(
[0] => Array
(
[order_id] => 2646
[prod_code] => 2811
[order_qty] => 1
)
[1] => Array
(
[order_id] => 2646
[prod_code] => 2812A
[order_qty] => 3
)
[2] => Array
(
[order_id] => 2647
[prod_code] => 2812A
[order_qty] => 2
)
[3] => Array
(
[order_id] => 2647
[prod_code] => 2810
[order_qty] => 2
)
[4] => Array
(
[order_id] => 2650
[prod_code] => 2906
[order_qty] => 1
)
[5] => Array
(
[order_id] => 2650
[prod_code] => 2908
[order_qty] => 6
)
[6] => Array
(
[order_id] => 2650
[prod_code] => 2909
[order_qty] => 3
)
[7] => Array
(
[order_id] => 2650
[prod_code] => 2913
[order_qty] => 1
)
[8] => Array
(
[order_id] => 2658
[prod_code] => 2880
[order_qty] => 3
)
[9] => Array
(
[order_id] => 2658
[prod_code] => 2881
[order_qty] => 3
)
[10] => Array
(
[order_id] => 2658
[prod_code] => 2882
[order_qty] => 1
)
)
我在解决问题时如何绕过order_items
数组并将prod_code
和order_qty
值插入order
数组{{1}值匹配。这是我到目前为止所做的。
order_id
我的问题是,当我们查看新的// create orders array - key by order_id
while ($order = mysqli_fetch_assoc($order_rows)) {
$orders[$order['order_id']] = $order;
}
// create the order_items array
while ($item = mysqli_fetch_assoc($item_rows)) {
$order_items[] = $item;
}
// update orders array with ordered items
$item_index = 1;
foreach ($order_items as $key => $value) {
// get the order id
$item_order_id = $value['order_id'];
// update orders array with ordered items grouped by order_id
if ($item_order_id == $orders[$item_order_id]['order_id']) {
$orders[$item_order_id]['prod_code_'.$item_index] = $value['prod_code'];
$orders[$item_order_id]['order_qty_'.$item_index] = $value['order_qty'];
// same order id so increment the counter
$item_index++;
} else {
// new order id start at 1
$item_index = 1;
}
}
时,计数器不会重置为1。
我试图为我的数组实现的输出是这样的。
所需订单数组输出
item_order_id
非常感谢任何反馈或帮助。
答案 0 :(得分:0)
你可以做两遍:
foreach($order_items as $order_id => $order_items) {
$record = ['order_id' => $order_id];
// add order items
foreach($order_items as $index => $order_item) {
foreach ($order_item as $attribute_name => $attribute_value) {
$key = sprintf('%s_%d', $attribute_name, $index + 1);
$record[$key] = $attribute_value;
}
}
// generate csv row with record here
}
$("#submit").click(function(e){//now add e to your function
e.preventDefault;//now prevent submit happen so it wont refresh
Location.reload();
$("#biscuit").biscuit({
Width : $("#width").val(),//you forgot one " here
BgColor : $("#BgColor").val(),//also you forgot one " here
});
});