1

I have an json array like given below, what I want is to parse through the array and get a value for corresponding key .Eg SubAdministrativeAreaName .I could have parsed it like .

["AddressDetails"]['Country']['AdministrativeArea'] ['SubAdministrativeArea']['SubAdministrativeAreaName']

but the structure of array is not fixed , it may contain some other keys within which "SubAdmininstrativeArea" may be enclosed .

What I want is a php function that will search for a particular key name through multidimensional json array of any depth .

Any help would be appreciated .

"AddressDetails": {
    "Accuracy": 6,
    "Country": {
        "AdministrativeArea": {
            "AdministrativeAreaName": "Maharashtra",
            "SubAdministrativeArea": {
                "SubAdministrativeAreaName": "Parbhani",
                "Thoroughfare": {
                    "ThoroughfareName": "SH217"
                }
            }
        },
        "CountryName": "India",
        "CountryNameCode": "IN"
    }
}

0

3 Answers 3

2

OK, different answer based on comment below:

function array_key_search_deep($needle, $haystack) {
    $value = NULL;
    if(isset($haystack[$needle])) {
        $value = $haystack[$needle];
    } else {
        foreach($haystack as $node) {
            if(is_array($node)) {
                $value = array_key_search_deep($needle, $node);
                if(!is_null($value)) {
                    break;
                }
            }
        }
    }
    return $val;
}

Old Answer

This will allow you to traverse an unknown path in any array tree:

$path = array('AddressDetails', 'Country', 'AdministrativeArea', 'SubAdministrativeArea', 'SubAdministrativeAreaName');

$node = $json_object;

foreach($path as $path_index) {
    if(isset($node[$path_index])) {
        $node = $node[$path_index];
    } else {
        $node = NULL;
    }
}

echo($node);
Sign up to request clarification or add additional context in comments.

2 Comments

That wouldn't cover the requirement "the structure of array is not fixed , it may contain some other keys within which "SubAdmininstrativeArea" may be enclosed"
make sure to update the new function to return $value instead of $val or it always returns NULL
1

Thanks guys , what I made was a a solution like this

I finally found a simple solution myself 

For eg) To get "ThoroughfareName" make a call 
recursive_array_search($json_array,'ThoroughfareName') ;



         function recursive_array_search($arr,$jackpot)
         {
          foreach ($arr as $key => $value)
          { 
            if(is_array($value))
            {
                $val=recursive_array_search($value,$jackpot) ;
                return $val;
            }
            else
            {
                    if($key==$jackpot)
                    return $value;
            }
          }
         }


Comments

0

You could use JSONPath (XPath for JSON)

http://goessner.net/articles/JsonPath/

(with for instance the expression "$..SubAdministrativeAreaName")

EDIT: Haven't tested it, not sure how reliable it is.

Comments

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.