PHP中的数组合并

PHP中的数组合并,php,arrays,array-combine,Php,Arrays,Array Combine,我试图在数组中组合键和值。我有一个不同价格的产品标识。 比方说 Product id and price id 101 and price is 100 id 105 and price is 200 id 101 and price is 300 带有$product\U ID[]的阵列中的产品ID列表以及价格列表$price\U amount[] 因此,我更喜欢使用array\u combine 我制作了array\u combine($product\u id,$price\u

我试图在数组中组合键和值。我有一个不同价格的产品标识。 比方说

Product id and price 

id 101 and price is 100

id 105 and price is 200

id 101 and price is 300
带有
$product\U ID[]
的阵列中的产品ID列表以及价格列表
$price\U amount[]
因此,我更喜欢使用
array\u combine

我制作了
array\u combine($product\u id,$price\u 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);

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

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

认为您正在寻找类似的内容。我已经有一段时间没有使用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;
    }
}

以为你在找这样的东西。我已经有一段时间没有使用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;
    }
}

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

array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)
您要查找的是关联数组。您可以明确指定所需的键,并在该键处存储所需的值


下面是一个有用的

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

array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)
您要查找的是关联数组。您可以明确指定所需的键,并在该键处存储所需的值


这里有一个可能会有帮助的方法,恐怕您必须手动执行此操作:

$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];
    }
}

恐怕您必须手动执行此操作:

$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];
    }
}

array\u 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\u 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);
?>
试试这个

$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];
}
$final_arr=array();
对于($i=0;$i试试这个

$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];
}
$final_arr=array();
对于($i=0;$i
简单的代码,您可以清楚地看到发生了什么:

$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];
}
简单的代码,您可以清楚地看到发生了什么:

$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];
}

问题是他有重复的产品ID,所以索引会被覆盖。问题是他有重复的产品ID,所以索引会被覆盖。你怎么能让
101
重复
$product\u id
?@ICanHasCheezburger
$product\u id=array(101,101)
@更新请展示带有实际工作代码的源数组示例,它与文本描述混淆。为什么id 101有两个价格标签100和300?即使如此,如果是数据库记录,我想你应该将价格和
按id分组
@Bsienn它是可变价格你怎么能在
$产品标识
?@ICanHasCheezburger
$product\u id=array(101101)
@fresh请展示带有实际工作代码的源数组示例,它与文本描述混淆。为什么id 101有两个价格标签100和300?即使如此,如果它是数据库记录,我想你应该将你的价格和
按id分组
@Bsienn它是可变价格谢谢你的回答。我有一个产品id,如rray类数组(101105101)和price类数组(100300200)在本例中,我想组合数组,但默认情况下它忽略了重复的id(默认行为),因此在本例中,如果存在相同的id,我想添加值。感谢您的回复,我的id 101Hi为100+300。我有一个产品id,如array类数组(101105101)中所示和price as array(100300200)在本例中,我希望合并数组,但默认情况下它忽略了重复id(默认行为),因此在本例中,如果存在相同的id,我希望添加值,我的意思是将100+300添加到id 101