Captain Hypertext
Captain Hypertext

Reputation: 2506

How to deeply search php object and pull all instances of a certain property?

I'm having difficulty finding a good solution or a previous question for this since it's difficult to put into words, but I have a json object here:

{

    "type":"template",
    "customStyle":false,
    "preheaderVisible":true,
    "titleText":"Email Template",
    "mainBlocks":{
        "type":"blocks",
        "blocks":[
            {
                "type":"singleArticleBlock",
                "image":{
                    "type":"image",
                    "src":"/template/image/logo-large.png",
                    "url":"http://example.com/",
                    "alt":""
                },
                "longText":"\n <p>Far far away, behind the word mountains, far from the countries <a href=\"\">Vokalia and Consonantia</a>, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.</p>\n ",
                "buttonLink":{
                    "type":"link",
                    "text":"BUTTON",
                    "url":"http://example.com/"
                }
            },
            {
                "type":"tripleArticleBlock",
                "leftImage":{
                    "type":"image",
                    "src":"/template/image/logo-small.png",
                    "url":"http://example.com/",
                    "alt":""
                },
                "leftLongText":"\n <p>Far far away, behind the word mountains, far from the countries <a href=\"\">Vokalia and Consonantia</a>, there live the blind texts. </p>\n ",
                "leftButtonLink":{
                    "type":"buttonLink",
                    "text":"BUTTON",
                    "url":"http://example.com/"
                },
                "middleImage":{
                    "type":"image",
                    "src":"/template/image/logo-small.png",
                    "url":"http://example.com/",
                    "alt":""
                },
                "middleLongText":"\n <p>Far far away, behind the word mountains, far from the countries <a href=\"\">Vokalia and Consonantia</a>, there live the blind texts. </p>\n ",
                "middleButtonLink":{
                    "type":"buttonLink",
                    "text":"BUTTON",
                    "url":"http://example.com/"
                },
                "rightImage":{
                    "type":"image",
                    "src":"",
                    "url":"http://example.com/",
                    "alt":""
                },
                "rightLongText":"\n <p>Far far away, behind the word mountains, far from the countries <a href=\"\">Vokalia and Consonantia</a>, there live the blind texts. </p>\n ",
                "rightButtonLink":{
                    "type":"buttonLink",
                    "text":"BUTTON",
                    "url":"http://example.com/"
                }
            },
        ]
    }
}

I need to get every url property, regardless of the nest depth or sub-object it's in. So it could be the url for an image or leftButtonLink and at any depth. I figured there's got to be an easy "give me all props called x" function. What's the most efficient way to do this in php?

Upvotes: 0

Views: 44

Answers (2)

Chad
Chad

Reputation: 1189

This is untested, try something like this

$data = json_decode($jsondata, true);
$urls = array();
r_search($data);

function r_search($array) {
  global $urls;
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      r_search($value);
    } else if ($key === 'url') {
      $urls[] = $value;
    }
  }
}

There may also be a better way without globals. do some searching for recursive array search.

Upvotes: 1

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

array_walk_recursive should work well for this.

$data = json_decode($json, true);
$urls = [];
array_walk_recursive($data, function($value, $key) use (&$urls) {
    if ($key == 'url') $urls[] = $value;
});

Be sure to decode to arrays rather than objects by setting the second parameter of json_decode.

This won't tell you what the URL is for, though, if that matters. (e.g. image, leftButtonLink, etc.) This is just an easy "give me all the props called x" function.

Upvotes: 1

Related Questions