PHP - Search in array


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

Function to check existence of value in multidimensional array.


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

URL: http://www.softafzar.net

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.