/** * Search for a key-name from array or multi-dimensional array. * The first result found will be returned * * @param string $needle * @param array $haystack * @param boolean $strict [description] * * @return boolean */ function array_key_in($needle, array $haystack, $strict = false) { // if ( ! is_array($needle)) { // $needle = [$needle]; // } foreach ($haystack as $key => $value) { if (is_string($key) && $key == $needle) { return $value; } else { if (is_array($value)) { $result = array_key_in($needle, $value); if ($result) { return $result; } } } } return false; }
function run($title, $search_for, $haystack) { echo "<div class='row'>"; echo "<div class='col-md-12'>"; echo "<h2>{$title}</h2>"; $json_str = json_encode($haystack); // Visual Output echo '<code>'; echo "<b>Searching: \"{$search_for}\" in..</b>\n"; var_dump($json_str); $result = (bool) strstr($json_str, $search_for) ? 'True' : 'False'; echo "<strong>Result: {$result}</strong>"; // Process $result = array_key_in($search_for, $haystack); echo "<h2>Result</h2>"; var_dump($result); echo '</code>'; echo '</div>'; echo '</div>'; }