Posted By

noganno on 06/29/13


Tagged

php


Versions (?)

Рекурсия в php


 / Published in: PHP
 

URL: http://phpmaster.com/understanding-recursion/

поиск значения в многомерном массиве

  1. function find_in_arr($key, $arr) {
  2. foreach ($arr as $k=>$val) {
  3. if($k == $key) {
  4. return $val;
  5. }
  6.  
  7. if(is_array($val)) {
  8. $result = find_in_arr($key, $val);
  9. if($result != false) {
  10. return $result;
  11. }
  12. }
  13. }
  14. return false;
  15. }
  16.  
  17.  
  18.  
  19.  
  20. $arr = array(
  21. 'name' => 'Php Master',
  22. 'subject' => 'Php',
  23. 'type' => 'Articles',
  24. 'items' => array(
  25. 'one' => 'Iteration',
  26. 'two' => 'Recursion',
  27. 'methods' => array(
  28. 'factorial' => 'Recursion',
  29. 'fibonacci' => 'Recursion',
  30. ),
  31. ),
  32. 'parent' => 'Sitepoint',
  33. );
  34.  
  35. var_dump(find_in_arr('fibonacci', $arr));

Report this snippet  

You need to login to post a comment.