递归函数不查找嵌套项

时间:2015-07-04 04:12:34

标签: php json recursion

我收到一个json对象作为网站的响应,我试图找到一个字符串的匹配,无论它嵌套多深。目前,这适用于对象的第一级中的任何内容,但是当我在第二级尝试某些内容时,它似乎不起作用。这是我第一次尝试递归函数,所以我可能只是想错了:

foreach($parseObj as $msg) {
    parseBlock($msg,'SEARCH STRING',$refID);
}

function parseBlock($block,$id,&$refID) {
    if (isset($block->data->id)) {
        echo '<b>Parsing: ' . $block->data->id . ':</b><br/> ';
    }
    if (isset($block->data->body)) {
        if (strpos($block->data->body,$id) !== false) {
            echo 'found it - <br/>';
            $refID = $block->data->name;
            return $refID;
        } else {
            echo 'not here<br/>';
        }
    }
    if (isset($block->data->children)) {
        foreach($block->data->children as $msg) {
            parseBlock($msg,$id,$refID);
        }
    }
    if (isset($block->data->replies->data->children)) {
        foreach($block->data->replies->data->children as $msg) {
            parseBlock($msg,$id,$refID);
        }
    }
}

当我想要的项目嵌套在第二层或更深层时,它会在此行中找到ID:

echo '<b>Parsing: ' . $block->data->id . ':</b><br/> ';

我知道我想要的字符串(&#39; SEARCH STRING&#39;)列在那里,因为我可以在浏览器中看到它,但它告诉我&#34;不在这里&#34;

当它在第一个嵌套级别时,它会响应&#34;找到它&#34;

如何让这个工作适用于更深层次的嵌套级别?

以下是项目嵌套深度超过1级时JSON对象的示例:

[
  {
    "kind": "Listing",
    "data": {
      "modhash": "pdyhr8d2dgf5ffd0f279801a563bc45cdfd0fd52fb2caa3c86",
      "children": [
        {
          "kind": "t3",
          "data": {
            "domain": "rankery.com",
            "banned_by": null,
            "media_embed": {

            },
            "subreddit": "test",
            "selftext_html": null,
            "selftext": "",
            "likes": true,
            "suggested_sort": null,
            "user_reports": [

            ],
            "secure_media": null,
            "link_flair_text": null,
            "id": "39tnux",
            "from_kind": null,
            "gilded": 0,
            "archived": false,
            "clicked": false,
            "report_reasons": null,
            "author": "rankery",
            "media": null,
            "name": "t3_39tnux",
            "score": 2,
            "approved_by": null,
            "over_18": false,
            "hidden": false,
            "thumbnail": "default",
            "subreddit_id": "t5_2qh23",
            "edited": false,
            "link_flair_css_class": null,
            "author_flair_css_class": null,
            "downs": 0,
            "mod_reports": [

           ],
            "secure_media_embed": {

            },
            "saved": false,
            "removal_reason": null,
            "from": null,
            "is_self": false,
            "from_id": null,
            "permalink": "/r/test/comments/39tnux/rb_test/",
            "stickied": false,
            "created": 1434307698.0,
            "url": "http://www.rankery.com/redditBot1.php",
            "author_flair_text": null,
            "title": "RB TEST",
            "created_utc": 1434304098.0,
            "ups": 2,
            "upvote_ratio": 1.0,
            "num_comments": 21,
            "visited": false,
            "num_reports": null,
            "distinguished": null
          }
        }
      ],
      "after": null,
      "before": null
    }
 },
  {
    "kind": "Listing",
    "data": {
      "modhash": "pdyhr8d2dgf5ffd0f279801a563bc45cdfd0fd52fb2caa3c86",
      "children": [
        {
          "kind": "t1",
          "data": {
            "subreddit_id": "t5_2qh23",
            "banned_by": null,
            "removal_reason": null,
            "link_id": "t3_39tnux",
            "likes": true,
            "replies": {
             "kind": "Listing",
             "data": {
                "modhash":     "pdyhr8d2dgf5ffd0f279801a563bc45cdfd0fd52fb2caa3c86",
                "children": [
                  {
                    "kind": "t1",
                    "data": {
                     "subreddit_id": "t5_2qh23",
                     "banned_by": null,
                     "removal_reason": null,
                      "link_id": "t3_39tnux",
                      "likes": true,
                      "replies": "",
                      "user_reports": [

                      ],
                     "saved": false,
                      "id": "csrd4vg",
                      "gilded": 0,
                      "archived": false,
                      "report_reasons": null,
                      "author": "rankery",
                      "parent_id": "t1_cspyeux",
                      "score": 1,
                      "approved_by": null,
                      "controversiality": 0,
                      "body": "SEARCH STRING",
                      "edited": 1435959047.0,
                      "author_flair_css_class": null,
                      "downs": 0,
                      "body_html": "&lt;div class=\"md\"&gt;&lt;p&gt;&lt;a     href=\"http://www.rankery.com/incl/redditBot/addRanking.phpid=81\"&gt;TEST&lt;/a&gt;&lt;/p&gt;\n&lt;/div&gt;",
                      "subreddit": "test",
                      "score_hidden": false,
                      "name": "t1_csrd4vg",
                      "created": 1435986571.0,
                      "author_flair_text": null,
                      "created_utc": 1435957771.0,
                     "distinguished": null,
                      "mod_reports": [

                      ],
                      "num_reports": null,
                      "ups": 1
                    }
                  }
                ],
                "after": null,
                "before": null
              }
            },
            "user_reports": [

            ],
            "saved": false,
           "id": "cspyeux",
            "gilded": 0,
            "archived": false,
            "report_reasons": null,
            "author": "rankery",
            "parent_id": "t1_csa56v2",
            "score": 1,
            "approved_by": null,
            "controversiality": 0,
            "body": "Random+String%3A+q6K1CmU5FnpW3JO0ij7d9RYPGeZwl24A",
            "edited": false,
            "author_flair_css_class": null,
            "downs": 0,
            "body_html": "&lt;div class=\"md\"&gt;&lt;p&gt;Random+String%3A+q6K1CmU5FnpW3JO0ij7d9RYPGeZwl24A&lt;/p&gt;\n&lt;/div&gt;",
            "subreddit": "test",
            "score_hidden": false,
            "name": "t1_cspyeux",
            "created": 1435855800.0,
            "author_flair_text": null,
            "created_utc": 1435852200.0,
            "distinguished": null,
            "mod_reports": [

            ],
            "num_reports": null,
            "ups": 1
             }
          }
      ],
      "after": null,
      "before": null
    }
  }
]

编辑:添加了JSON对象示例

编辑2:添加&#39;搜索字符串&#39;进入JSON对象以匹配顶部的示例

1 个答案:

答案 0 :(得分:2)

看起来body是网址编码的,所以请尝试:

if (strpos(urldecode($block->data->body),$id) !== false) {