Sunday 15 September 2013

PHP JSON Check if value exists -



PHP JSON Check if value exists -

this question has reply here:

php multi dimensional array search 12 answers

ok have json string looks this.

{ "1": [ "a", "ab", "ac" ], "3": [ "v", "aw", "ea" ], "4": [ "ffg", "sd" ] }

i decoded json_decode($string, true); now, need find way check, example, a exists? if exists, want know name of parent value(e.g. 1). keys , values aren't same, maintain changing. can provide me illustration code on how can this? in advanced ;)

that string gives me null, if tried convert json, i've built array example. i've create function, , loop through on key of that. after that, in sub array, loop through 1 time again check first occurence of searched string. if need results, can strore keys in array, , homecoming that.

//the string search $searchstring = "aw"; //the array search $array = array( "1" => array("a", "ab", "ac"), "3" => array("v", "aw", "ea"), "4" => array("ffg", "sd") ); $result = getarrayindex($array, $searchstring); if (empty($result)) { "i did not find string: " . $searchstring; } else { echo "the index of main array, '" . $searchstring . "' found is: " . $result; } function getarrayindex($array, $searchstring) { if (count($array)) { foreach (array_keys($array) $key) { if (is_array($array[$key])) { foreach ($array[$key] $item) { if ($item === $searchstring) { homecoming $key; } } } } } homecoming false; }

php json

No comments:

Post a Comment