1

I am trying to combine keys and values in arrays. I have an product_id with different price. Let say

Product id and price 

id 101 and price is 100

id 105 and price is 200

id 101 and price is 300

list of product ids in array with $product_ids[] and list of price also $price_amount[] So I preferred to combine the two arrays using array_combine

I made array_combine($product_ids,$price_amount); Now it appears look like this way

array(2) { [101]=> float(100) [105]=> float(300) } 

Is there is a way to add the key elements to the id as something like

array(2) {
    [101] => float(400) (100+300)
    [105] => float(300)
}

Here is the idea i tried

 $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);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Vignesh Pichamani
  • 7,950
  • 22
  • 77
  • 115

7 Answers7

2

You will have to do this manually I'm afraid:

$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];
    }
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

PHP arrays are associative so you can write something like: price['101'] = 100 thereby using the product id as the array index.

bitfiddler
  • 2,095
  • 1
  • 12
  • 11
1

Thinking you are looking for something like this. I haven't done php in awhile, so the syntax may need tweaking, but I think the logic is correct.

$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;
    }
}
John
  • 763
  • 1
  • 10
  • 21
  • Hi Thanks for your reply. i have a product ids as in array like array(101,105,101) and price as array(100,300,200) in this case i want to combine the array but it omitting the duplicate id by default (default behavior) so in this case i want to add the values if the same id is present i mean 100+300 to the id 101 – Vignesh Pichamani Mar 25 '14 at 06:04
1

array_combine will not do the trick for you. You will have to iterate through the array and total them as you go. Here's an example:

<?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);
?>

Results:

Array
(
    [101] => 400
    [105] => 200
)
zedfoxus
  • 35,121
  • 5
  • 64
  • 63
1

Try this

$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];
}
rakeshjain
  • 1,791
  • 11
  • 11
1

Simple code so you can see what's happening clearly:

$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];
}
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
0

Yes you can add key elements to id, basically an array can be created by using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

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

the one you are looking for is associative array. you can definitely specify the key you want and store the value you want at that key.

here is a link that would be helpful

krishna
  • 4,069
  • 2
  • 29
  • 56
opensource-developer
  • 2,826
  • 4
  • 38
  • 88