nested Array search by value or key


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



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. //PHP 5.3
  4.  
  5. function searchNestedArray(array $array, $search, $mode = 'value') {
  6.  
  7. foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) {
  8. if ($search === ${${"mode"}})
  9. return true;
  10. }
  11. return false;
  12. }
  13.  
  14. $data = array(
  15. array('abc', 'ddd'),
  16. 'ccc',
  17. 'bbb',
  18. array('aaa', array('yyy', 'mp' => 555))
  19. );
  20.  
  21. var_dump(searchNestedArray($data, 555));

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.