在PHP中组合多个数组
下面的PHP代码不包括Google Merchant Center中的Woocommerce类别。您如何将在PHP中组合多个数组,php,arrays,woocommerce,Php,Arrays,Woocommerce,下面的PHP代码不包括Google Merchant Center中的Woocommerce类别。您如何将组合到_数组中以缩短代码 // Exclude categories from my Google Product Feed function lw_gpf_exclude_product($excluded, $product_id, $feed_format) { // Return TRUE to exclude a product, FALSE to include it,
组合到_数组中
以缩短代码
// Exclude categories from my Google Product Feed
function lw_gpf_exclude_product($excluded, $product_id, $feed_format) {
// Return TRUE to exclude a product, FALSE to include it, $excluded to use the default behaviour.
$cats = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
if ( in_array( 60, $cats ) ) {
return TRUE;
}
if ( in_array( 63, $cats ) ) {
return TRUE;
}
if ( in_array( 88, $cats ) ) {
return TRUE;
}
if ( in_array( 89, $cats ) ) {
return TRUE;
}
return $excluded;
}
add_filter( 'woocommerce_gpf_exclude_product', 'lw_gpf_exclude_product', 11, 3);
如果只有一个值足以返回true,则可以使两个数组相交,并且如果生成的数组具有大小(包含元素),则两个数组中至少存在一个值
return count(array_intersect([60, 63, 88, 89], $cats)) > 0;
如果只有一个值足以返回true,则可以使两个数组相交,并且如果生成的数组具有大小(包含元素),则两个数组中至少存在一个值
return count(array_intersect([60, 63, 88, 89], $cats)) > 0;
使用
array\u intersect
函数的解决方案:
...
if (array_intersect([60, 63, 88, 89], $cats)){
return TRUE;
}
使用
array\u intersect
函数的解决方案:
...
if (array_intersect([60, 63, 88, 89], $cats)){
return TRUE;
}
试试这个
return !empty(array_intersect(array(60,63,88,89),$cats));
试试这个
return !empty(array_intersect(array(60,63,88,89),$cats));
使用这样的函数可以缩短代码
function in_array_any($needles, $haystack) {
return !!array_intersect($needles, $haystack);
}
if(in_array_any([60,63,88,89], $cats)){
return TRUE;
}
数组中的函数任意($Pineers,$haystack){
return!!数组相交($pines,$haystack);
}
如果(在数组中任何([60,63,88,89],$cats)){
返回TRUE;
}
使用这样的函数可以缩短代码
function in_array_any($needles, $haystack) {
return !!array_intersect($needles, $haystack);
}
if(in_array_any([60,63,88,89], $cats)){
return TRUE;
}
数组中的函数任意($Pineers,$haystack){
return!!数组相交($pines,$haystack);
}
如果(在数组中任何([60,63,88,89],$cats)){
返回TRUE;
}