fork(1) download
  1. <?php
  2.  
  3. $arr = [
  4. 'vehicle info' => [
  5. 'one' => ['submodel' => 'LX', 'engine' => '2.3'],
  6. 'two' => ['color' => 'blue', 'year' => '2007', 'wheels' => '4'],
  7. 'three' => ['submodel' => 'LX', 'make' => 'Ford', 'model' => 'F-150', 'offroad' => 'No'],
  8. ],
  9. ];
  10.  
  11. function find($needle, $haystack, $found = '')
  12. {
  13. foreach ($haystack as $key => $value) {
  14. if ($found) {
  15. break;
  16. }
  17. if ($key === $needle) {
  18. $found = "{$needle} is {$value}";
  19. break;
  20. }
  21. if (is_array($value)) {
  22. $found = find($needle, $value, $found);
  23. }
  24. }
  25. return $found;
  26. }
  27.  
  28. $multiple_keys = array('submodel', 'year');
  29.  
  30. foreach ($multiple_keys as $wanted) {
  31. var_dump(find($wanted, $arr));
  32. }
  33.  
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
string(14) "submodel is LX"
string(12) "year is 2007"