Рекурсия в php


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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));

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.