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

时间:2018-10-13 10:18:32

标签: php arrays woocommerce counting

我坚持如何计算数组中的项目

代码

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

我需要返回$deposit_items中列出的具有产品ID的数组中的项目数

4 个答案:

答案 0 :(得分:3)

尝试

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

答案 1 :(得分:2)

类似这样的东西

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

可以将其压缩(打高尔夫球)成单行,如下所示:

$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

Array列,使用我的示例(有点简单)来获取大的多维数组并重新运行单个列:

$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

如果您使用diff数组翻转数组,您将获得购物车中没有的存款项目数量。

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

Not in cart

干杯!

答案 2 :(得分:0)

如果我正确地认为您的$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 
        }

    }

}

答案 3 :(得分:-1)

这个家伙想获得商务购物车中的商品数量,所以这是解决方案。

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