使用 strpos 和 haystack 作为数组(在 WordPress 中)


Using strpos with haystack as an array (in WordPress)?

有没有办法使用 strpos() 将$haystack从数组转换为字符串?我的临时解决方案是改用 in_array(),但它不太理想:

function dynamic_id() {
    $find_str = get_body_class();
    if ( in_array( 'single-sfwd-courses', $find_str ) || in_array( 'single-sfwd-lessons', $find_str ) || in_array( 'single-sfwd-topic', $find_str) || in_array( 'single-sfwd-quiz', $find_str ) ) {
        echo 'id="jumbo_bg1"';
        echo '><style type="text/css">
        .jumbotron a.btn {
            display: none;
        }
        </style';
    } else {
        echo 'id="jumbo_bg"';
    }
}

任何帮助将不胜感激。谢谢!

看起来您正在尝试查看数组中是否有任何这些值。 在这种情况下,我会尝试创建一个值数组,然后使用array_intersect()查看它们中的任何一个是否匹配。

如果 array_intersect() 返回至少具有一个值的数组,则表示存在匹配项。

function dynamic_id() {
    $find_str = get_body_class();
    $vals = array('single-sfwd-courses', 'single-sfwd-lessons', 'single-sfwd-topic', 'single-sfwd-quiz');
    $intersect = array_intersect($find_str, $vals);
    if(count($intersect) > 0){
        // There was a match
    }
    else{
        // No match
    }
}