0

I am getting a json object as a response from a website, and I am trying to find a match to a string, no matter how deep it is nested. Currently, this works for anything in the first level of the object, but as soon as I try something in the second level it does not seem to work. This is my first attempt at a recursive function, so I may just be thinking about it wrong:

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);
        }
    }
}

When the item I want is nested 2nd level or deeper, it finds the ID just fine with this line:

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

And I know the string I want ('SEARCH STRING') is listed there because I can see it in a browser, but it tells me "not here"

When it is on the first nesting level, it responds "found it"

How can I make this work for the deeper nested levels?

Here is an example of the JSON object when the item is nested more than 1 level deep:

[
  {
    "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
    }
  }
]

EDIT: Added JSON Object Example

EDIT 2: ADDED 'SEARCH STRING' into the JSON object to match the example at the top

4
  • When I try it, it says: Parsing: csrd4vg: found it. That ID is nested. Commented Jul 4, 2015 at 4:44
  • @Barmar Whoa, I wonder why I am getting a different response on my side, thats exactly what I'm trying to get, csrd4vg is right. I guess I'm maybe presenting the search string wrong? the search string is actually a url, do I need to encode it in any way do you think? Commented Jul 4, 2015 at 4:50
  • In the cspyeux object, the body is URL-encoded. You may need to use urlencode($id) when you search for it. Commented Jul 4, 2015 at 4:53
  • Or use urldecode($block->data->body) Commented Jul 4, 2015 at 4:54

1 Answer 1

2

It looks like body is URL-encoded, so try:

if (strpos(urldecode($block->data->body),$id) !== false) {
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's exactly what was happening, thanks so much, I'm glad I wasn't too far off with the recursive function in general. You're a lifesaver, I spent way too much time looking for a more complicated error!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.