3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ "1" => [ "book" => "Harry Potter", "artist" => array("David", "Emma"), "country" => [ ["description" => "Wander"], ["description" => "Magic"] ] ], "2" => [ "book" => "Science book", "artist" => array("Artist 1", "Melanie Hudson"), "country" => [ ["description" => "Physics"], ["description" => "Albert Einstein"] ] ], "3" => [ "book" => "Bible", "artist" => array("Artist 1", "Pedro"), "country" => [ ["description" => "Love"], ["description" => "Respect"] ] ], ]; $result = []; // $result is container for matches - filled by reference $needle = 'Wander'; // the key we are looking for recurse($data, $needle, $result); function recurse($haystack = [], $needle = '', &$result) { foreach($haystack as $key => $value) { if(is_array($value)) { recurse($value, $needle, $result); } else { if(strpos($value, $needle) !== false) { $result[] = $value; // store match } } } } echo '<pre>'; var_dump($result); echo '</pre>';

preferences:
31.07 ms | 405 KiB | 5 Q