Search code examples
phparrayswoocommercecounting

PHP Count number of array items that exist in another array


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


Solution

  • 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!