1

我正在尝试在数组中组合键和值。我有一个不同价格的 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);
4

7 回答 7

2

恐怕您将不得不手动执行此操作:

$total = array();
foreach ($product_ids as $key => $value) {
    // each value of product_ids becomes the key
    if (isset($total[$value])) {
        // we have seen this key before
        $total[$value] += $price_amount[$key];
    } else {
        // new key
        $total[$value] = $price_amount[$key];
    }
}
于 2014-03-25T06:02:03.587 回答
1

PHP 数组是关联的,因此您可以编写如下内容:price['101'] = 100从而使用产品 ID 作为数组索引。

于 2014-03-25T05:52:49.763 回答
1

array_combine不会为您解决问题。您将不得不遍历数组并随时汇总它们。这是一个例子:

<?php
$product_ids = array('101', '105', '101');
$price_amount = array(100, 200, 300);
$combined = array();

$productCount = count($product_ids);
for($i = 0; $i < $productCount; $i++) {

    // if this product_id is not in the $combined array, add its price
    // as associative array ('101'=>100)
    // but if it is found in $combined, add to its current price
    if (!array_key_exists($product_ids[$i], $combined)) {
        $combined[$product_ids[$i]] = $price_amount[$i];
    } else {
        $combined[$product_ids[$i]] += $price_amount[$i]; 
    }
}

print_r($combined);
?>

结果:

Array
(
    [101] => 400
    [105] => 200
)
于 2014-03-25T06:07:38.347 回答
1

认为你正在寻找这样的东西。我有一段时间没有做 php,所以语法可能需要调整,但我认为逻辑是正确的。

$cart = array(
    "101" => 100, 
    "105" => 200, 
    "101" => 300
);

$product_id_arr = array();

foreach ($cart as $product_id => $price) {
    if(array_key_exists($product_id, $product_id_arr)){
        $product_id_arr[$product_id] = $product_id_arr[$product_id] + $price;
    }else{
        $product_id_arr[$product_id] = $price;
    }
}
于 2014-03-25T05:59:21.540 回答
1

尝试这个

$final_arr = array();
for($i=0;$i<count($product_ids);$i++) {
    if(!isset($final_arr[$product_ids[$i]])) {
        $final_arr[$product_ids[$i]] = 0;
    }
    $final_arr[$product_ids[$i]] += $price_amount[$i];
}
于 2014-03-25T06:09:58.407 回答
1

简单的代码,您可以清楚地看到发生了什么:

$ids    = array(101, 105, 101);
$prices = array(100, 200, 300);

$totals = array();
foreach ($ids as $key => $id)
{
    // Make sure index is defined
    if ( ! isset($totals[$id]))
    {
        // Make sure we have a value
        $totals[$id] = 0;
    }

    // Assuming index $key matches... add to the total
    $totals[$id] += $prices[$key];
}
于 2014-03-25T06:10:05.530 回答
0

是的,您可以将关键元素添加到 id,基本上可以使用array()语言构造创建一个数组。它接受任意数量的逗号分隔key => value对作为参数。

array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)

您正在寻找的是关联数组。您绝对可以指定所需的密钥并将所需的值存储在该密钥处。

这是一个有用的链接

于 2014-03-25T05:59:25.813 回答