PHP 计算另一个数组中存在的数组项的数量

PHP Count number of array items that exist in another array

我对如何计算数组中的项目感到困惑

代码

// 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 
    }
}

我需要 return 数组中具有 $deposit_items

中所列产品 ID 的项目数

尝试

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

像这样

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

例如

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

输出

2

Sandbox

它可以像这样被压缩 (Golfed) 成一行:

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

供参考

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

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

数组列,获取那个大的多维数组并返回单个列,使用我的(有点简单)示例:

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

变成(我加了333,只是为了好玩)

  [4359, 4320, 333]

与您的 $deposit_items 格式基本相同。一旦它们相同,我们就可以使用数组相交,找到数组 1 中出现在第二个数组中的所有项目。在这种情况下,我们需要上述数组项,前提是它们在 $deposit_items 中。一旦我们有了这些,用 count 来计算它们就很简单了。

Sandbox

进一步说明

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

输出

[4359, 4320, 333]

然后

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

输出

 [4359, 4320]

然后计数非常困难。

其他

巧合的是,如果您想反过来,计算不在 $deposit_items 中的项目,您只需将 array_intersect 替换为 array_diff

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

Not in Deposit Items

如果您使用数组差异翻转数组,您将获得不在购物车中的存款项目数。

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

Not in cart

干杯!

这家伙想要获得 woocommerce 购物车的商品数量,所以这就是解决方案。

<?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>";
    } 
?>

如果我没看错,你的 $item_values['product_id'] 本身就是一个数组,你可以尝试类似的方法:

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

    }

}