Posted By

Sadeveloper on 12/29/13


Tagged

php search inarray search-in-array


Versions (?)

PHP - Search in array


 / Published in: PHP
 

URL: http://www.softafzar.net

Function to check existence of value in multidimensional array.

  1. $array = array(
  2.  
  3. 0 => array(
  4. "fruit" => "orange",
  5. "id" => 4
  6. ),
  7. 1 => array(
  8. "test" => "test1",
  9. "id" => 152
  10. ),
  11. 2 => array(
  12. "test" => "test2",
  13. "id" => 152
  14. )
  15. );
  16.  
  17.  
  18.  
  19. function search_in_array($value, $array) {
  20. if(in_array($value, $array)) {
  21. return true;
  22. }
  23. foreach($array as $item) {
  24. if(is_array($item) && search_in_array($value, $item))
  25. return true;
  26. }
  27. return false;
  28. }
  29.  
  30. $so = (search_in_array('orange',$array)) ? 'nothing' : 'daa';
  31. echo $so;

Report this snippet  

You need to login to post a comment.