duanhui3759 2014-12-24 19:55
浏览 80
已采纳

使用带有haystack的strpos作为数组(在WordPress中)?

Is there a way to use strpos() with the $haystack converted from an array to a string? My temporary solution was to use in_array() instead, but it is less than ideal:

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

Any help would be much appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • doushi4633 2014-12-24 20:20
    关注

    It looks like you are trying to see if any of those values in in your array. In that case, I'd try to make an array of values then use array_intersect() to see if any of them match.

    If array_intersect() returns an array with at least one value, that means there was a match.

    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
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题