drrrdo0802 2013-12-20 09:47
浏览 26
已采纳

如果strpos不完全错误,如何返回true?

I know how strpos works, and it is working as expected, but the return in the if function isn't.

Some code:

foreach ($socialstream as $key => $social) {
    //filtering in here
    $result= $this->search_objects($social);
    ....
}

My function search_objects:

function search_objects($objects)
{
    $filters = array('word', 'test');
    foreach ($objects as $key => $value) {
        if (is_array($value) || is_object($value)) {
            $this->search_objects($value);
        } else {
            //look for faulty strings in value
            foreach ($filters as $filter) {
                if (!is_int($value) && strpos($value, $filter) !== false) {
                    return true;
                }
            }

        }
    }
    return false;
}

If I print out my $result, I always get false back, instead of the true in the if function. I know it gets to the if when the needle exists in the haystack, by debugging, it's just not returning it.

What am I missing?

  • 写回答

1条回答 默认 最新

  • douju8782 2013-12-20 10:00
    关注

    I think your problem has to do with the recursive part:

        if (is_array($value) || is_object($value)) {
            $this->search_objects($value);
        }
    

    You probably want to do something with the return value. Like: if ($this->search_objects($value)) return true;

    (Then again, I'm not sure what you are trying to accomplish)

    edit: Try this:

    function search_objects($objects)
    {
        $filters = array('word', 'test');
        foreach ($objects as $key => $value) {
            if (is_array($value) || is_object($value)) {
                if ($this->search_objects($value)) {
                    return true;
                }
            } else {
                //look for faulty strings in value
                foreach ($filters as $filter) {
                    if (!is_int($value) && strpos($value, $filter) !== false) {
                        return true;
                    }
                }
    
            }
        }
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度