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);
5
  • How can you have 101 repeating in $product_ids? Commented Mar 25, 2014 at 5:57
  • 2
    @ICanHasCheezburger $product_ids = array(101, 101); Commented Mar 25, 2014 at 5:57
  • @Fresher please show examples of your source arrays with real working code, it is confusing with a text description. Commented Mar 25, 2014 at 5:59
  • how come an id 101 have two price tags 100 & 300? even so, if its a database record, IMO you should sum your price and group by id Commented Mar 25, 2014 at 6:09
  • @Bsienn It is variable price Commented Mar 25, 2014 at 6:11

7 Answers 7

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];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

1 Comment

Problem is he's got duplicate product ids, so the index would be overwritten.
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;
    }
}

1 Comment

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.