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

aydmsdu9  于 2023-02-21  发布在  PHP
关注(0)|答案(5)|浏览(191)

我被困在如何计算数组中的项
代码

// 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的项数

fgw7neuy

fgw7neuy1#

就像这样

// 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_diff替换array_intersect

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

Not in Deposit Items
如果你用arraydiff来翻转数组,你会得到不在购物车里的存款项目的数量。

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

Not in cart
干杯!

oiopk7p5

oiopk7p52#

试试看

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

fiei3ece3#

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

    }

}
f5emj3cl

f5emj3cl4#

我按照以下答案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);
irlmq6kh

irlmq6kh5#

这家伙想得到吴电子商务购物车的项目数量,所以这是解决方案。

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

相关问题