我正在尝试在数组中组合键和值。我有一个不同价格的 product_id。让我们说
Product id and price
id 101 and price is 100
id 105 and price is 200
id 101 and price is 300
数组中的产品 ID$product_ids[]
列表和价格列表$price_amount[]
所以我更喜欢使用组合这两个数组array_combine
我array_combine($product_ids,$price_amount);
现在看起来像这样
array(2) { [101]=> float(100) [105]=> float(300) }
有没有一种方法可以将关键元素添加到 id 中,例如
array(2) {
[101] => float(400) (100+300)
[105] => float(300)
}
这是我尝试过的想法
$products = array();
$order_totalss = array();
foreach (get_posts('post_type=shop_order&numberposts=-1&post_status=publish') as $order) {
$order = new WC_Order($order->ID);
if (wc_customer_bought_product($order->billing_email, $order->user_id, $product_id)) {
$productcounts[] = $product_id;
$order_totalss[] = $order->get_total();
}
}
$arraymergeme = array_combine($productcounts, $order_totalss);