数组合在PHP中

时间:2014-03-25 05:46:15

标签: php arrays array-combine

我正在尝试将数组中的键和值组合在一起。我有一个不同价格的product_id。 让我们说

Product id and price 

id 101 and price is 100

id 105 and price is 200

id 101 and price is 300

包含$product_ids[]的数组中的产品ID列表以及价格列表$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);

7 个答案:

答案 0 :(得分: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];
    }
}

答案 1 :(得分:1)

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

答案 2 :(得分: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;
    }
}

答案 3 :(得分: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
)

答案 4 :(得分: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];
}

答案 5 :(得分: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];
}

答案 6 :(得分:0)

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

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

您正在寻找的是关联数组。你绝对可以指定你想要的密钥,并将你想要的值存储在那个密钥上。

这是一个有用的link