Php 数组_intersect返回空值

Php 数组_intersect返回空值,php,woocommerce,array-intersect,Php,Woocommerce,Array Intersect,我试图将array1_ID与客户的购物车进行比较,如果阵列中的产品与购物车字段中的产品匹配,那么x_名称应该在那里,如果不匹配的话 应该走了。 我从购物车和array1_id中获取值,但当我将它们放入数组_intersect时,将导致NULL,从而使其始终返回true 这是我的密码: function wc_ninja_product_is_in_the_cart() { /*array 1*/ $array1_ids = array( '1', '3', '5');//field that s

我试图将array1_ID与客户的购物车进行比较,如果阵列中的产品与购物车字段中的产品匹配,那么x_名称应该在那里,如果不匹配的话 应该走了。 我从购物车和array1_id中获取值,但当我将它们放入数组_intersect时,将导致NULL,从而使其始终返回true

这是我的密码:

function wc_ninja_product_is_in_the_cart() {
/*array 1*/
$array1_ids = array( '1', '3', '5');//field that should
/*array 2*/
//$micro_ids = array( '2', '4');//fields that shouldnt come back

    // Products currently in the cart
    $cart_ids = array();
    $cart_categories = array();

    // Find each product in the cart and add it to the $cart_ids array
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $cart_product = $values['data'];
        $cart_ids[]   = $cart_product->id;
    }

    // If one of the special products are in the cart, return true.
    if ( ! array_intersect($array1_ids, $cart_ids) ) {
        echo "true: " , implode(';',$cart_ids);;//bug fixing
        return true;
    } else {
        return false;
        echo "false: " , implode(';',$cart_ids);;//bug fixing
    }
}
//Field Remover
function wc_ninja_remove_checkout_field( $fields ) {
    if ( ! wc_ninja_product_is_in_the_cart() )    {
        //removes Field x_name
        unset( $fields['billing']['x_name'] );
    }
    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' ); 
这应该起作用:

$hasSpecialProduct= false;    
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $cart_product = $values['data'];
    if (in_array($cart_product->id, $array1_ids)) {
        $hasSpecialProduct = true;
    }
    $cart_ids[]   = $cart_product->id;
}

// If one of the special products are in the cart, return true.
if ( $hasSpecialProduct ) {
    echo "true: " , implode(';',$cart_ids);;//bug fixing
    return true;
} else {
    return false;
}
您可以缩短最后一部分:

return $hasSpecialProduct;

您确定
$card\u id
中有元素吗?除非您通过数组运行它们,否则所有内容都会返回值。\u intersect()非常感谢您的帮助!我不能向上投票,因为我是新的堆栈溢出,但无论如何谢谢。不管怎样,我已经接受了你的回答。