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>';
Output for 8.4.1
Deprecated: recurse(): Optional parameter $haystack declared before required parameter $result is implicitly treated as a required parameter in /in/E6LT6 on line 34 Deprecated: recurse(): Optional parameter $needle declared before required parameter $result is implicitly treated as a required parameter in /in/E6LT6 on line 34 <pre>array(1) { [0]=> string(6) "Wander" } </pre>
Output for 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14
Deprecated: Optional parameter $haystack declared before required parameter $result is implicitly treated as a required parameter in /in/E6LT6 on line 34 Deprecated: Optional parameter $needle declared before required parameter $result is implicitly treated as a required parameter in /in/E6LT6 on line 34 <pre>array(1) { [0]=> string(6) "Wander" } </pre>
Output for 8.0.0 - 8.0.30
Deprecated: Required parameter $result follows optional parameter $haystack in /in/E6LT6 on line 34 <pre>array(1) { [0]=> string(6) "Wander" } </pre>
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
<pre>array(1) { [0]=> string(6) "Wander" } </pre>

preferences:
63.74 ms | 410 KiB | 5 Q