2

I'm stuck on how to count the items in my array

Code

// Product ID's to Search
$deposit_items = array( '4359', '4336', '4331', '4320' );

// Loop through the Cart Items
foreach ( $cart_object->get_cart() as $item_values ) {

    // Check if the cart contains items with the matching Product ID's
    if( in_array( $item_values['product_id'], $deposit_items )) {
        // Count number of products in Cart that have matching Product ID's 
    }
}

I need to return the number of items in the array that have Product ID's as listed in $deposit_items

3
  • what does $cart_object->get_cart() look like. What is the format of the data it returns. Commented Oct 13, 2018 at 10:19
  • $cart_object->get_cart() is a huge multidimensional array (WooCommerce) Commented Oct 13, 2018 at 10:26
  • How to filter a 2d array by comparing its column values against a flat whitelist: Keep array rows where a column value is found in a second flat array ...then you count(). Commented Apr 12, 2024 at 9:30

5 Answers 5

4

Something like this

// Product ID's to Search
$deposit_items = array( '4359', '4336', '4331', '4320' );
$cart_items = $cart_object->get_cart();
$cart_product_ids = array_column($cart_items, 'product_id');

$count = count(array_intersect($cart_product_ids, $deposit_items ));

For example

// Product ID's to Search
$deposit_items = array( '4359', '4336', '4331', '4320' );
$cart_items = [
    ['product_id' => 4359],
    ['product_id' => 4320]
];
$cart_product_ids = array_column($cart_items, 'product_id');

echo count(array_intersect($cart_product_ids, $deposit_items ));

Output

2

Sandbox

It can be condensed (Golfed) to a single line like this:

$deposit_items = array( '4359', '4336', '4331', '4320' );
echo count(array_intersect(array_column($cart_object->get_cart(),'product_id'),$deposit_items));

For reference

http://php.net/manual/en/function.array-column.php

http://php.net/manual/en/function.array-intersect.php

Array column, takes that big multi-dimensional array and retruns a single column, using my (somehwhat simple) example:

$cart_items = [
    ['product_id' => 4359],
    ['product_id' => 4320],
    ['product_id' => 333]
];

Becomes (I added 333, just for kicks)

  [4359, 4320, 333]

Basically the same format as your $deposit_items. Once they are the same we can use array intersect, to find all the items in array 1 that appear in the second array. In this case we want the above array items, only if they are in $deposit_items. Once we have those its a simple matter of counting them with count.

Sandbox

Just to illustrate it further

print_r([
    ['product_id' => 4359],
    ['product_id' => 4320],
    ['product_id' => 333]
], 'product_id');

Output

[4359, 4320, 333]

Then

print_r(array_intersect([4359, 4320, 333], ['4359', '4336', '4331', '4320']));

Output

 [4359, 4320]

And then count is pretty strait forward.

Other stuff

Coincidentally if you wanted to do the reverse, count the items NOT in $deposit_items you would simply replace array_intersect with array_diff.

 echo count(array_diff(array_column($cart_items, 'product_id'), $deposit_items ));

Not in Deposit Items

And if you flip the arrays around with array diff, you'd get the number of deposit items NOT in the cart.

 echo count(array_diff($deposit_items, array_column($cart_items, 'product_id')));

Not in cart

Cheers!

Sign up to request clarification or add additional context in comments.

4 Comments

Haha Lovely, but for someone who does not appear to get the concept of adding 1 to a counter in a loop, dont you think this would be a bit frightening :)
Perhaps, I like 1 liners though.
array_intersect and array_column in the background also running the loop. So according to me best way to do it is my own looping concept it would increase the performance as compared to single line code. If you ids in thousands. Trying running on web page or postman you will see the difference in milliseconds. But good inline code thumb ups for that
I added a bit more explanation to it. And yes they do a loop "in the background" but it's implemented in C so it's more efficient then foreach.
3

Try

$count = 0;

foreach ( $cart_object->get_cart() as $item_values ) {

    // Check if the cart contains items with the matching Product ID's
    if( in_array( $item_values['product_id'], $deposit_items )) {
        // Count number of products in Cart that have matching Product ID's 
      $count++;
    }
}

// Display
echo 'total number of matched items: ' , $count;

Comments

0

If i got it right that your $item_values['product_id'] is an array itself you could try something like:

$deposit_items = array( '4359', '4336', '4331', '4320' );

foreach ( $cart_object->get_cart() as $cart_items ){

    foreach ( $cart_items['product_id'] as $arr_values ){

        if ( in_array($arr_values, $deposit_items)){
            // Count number of products in Cart that have matching Product ID's 
        }

    }

}

Comments

0

I did this as in this answer https://stackoverflow.com/a/52791909/5864034

$array_of_id_items_to_be_checked = array_column($bigger_array_to_be_checked, 'id_item');

$id_items_to_find_their_occurrence = [11,18,2];

$intersects = array_intersect(
    $id_items_to_find_their_occurrence,
    $array_of_id_items_to_be_checked
);

$count_intersects = count($intersects);

Comments

-1

This guy wants to get woo commerce cart items quantity so this is the solution.

<?php



global $woocommerce;
$items = $cart_object->cart->get_cart();

    foreach($items as $item => $values) { 
        $_product =  wc_get_product( $values['data']->get_id()); 
        echo "<b>".$_product->get_title().'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
        $price = get_post_meta($values['product_id'] , '_price', true);
        echo "  Price: ".$price."<br>";
    } 
?>

3 Comments

Code-only answers are low-value on StackOverflow because they do a poor job of educating/empowering the OP and thousands of future researchers. Please always post an explanation of how your answer works and why it is a good idea. Please edit this answer asap.
because this user asks woocommerce problem (Wordpress plugin) and not explain very well as WordPress developer i understand the problem and post the solution.
There is never a good excuse to post code-only answers on this site. You are not the first person I am telling this to. Code-only answers are bad for this community. If you understand the question very well, then you will have no trouble explaining your answer. Please improve your post. If you are trying to earn points -- you will earn more if you explain your answer. If you are trying to help people -- you will help more people if you explain your answer.

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.