Quick actions

cmd+k|ctrl+k

Navigation

Languages

Search some text in PHP array

Snippet info

Language

Php

Visibility

public

Author

mertskaplan

Created

2016-11-04T17:10:17Z

Updated

2016-11-05T20:41:34Z

<?php

// Question: http://stackoverflow.com/questions/40428290/search-some-text-in-php-array

$namesArray = array('Peter', 'Joe', 'Glenn', 'Cleveland');  
if (in_array('Peter Parker', $namesArray)) {echo "There is.";}
else {echo "There is not.";}

/******************************************/
echo "\n\n";
// Solution: http://stackoverflow.com/a/40428522/4749879

function my_array_search($array, $needle){
  $matches = array_filter($array, function ($haystack) use ($needle){
    // return stripos($needle, $haystack) !== false; make the function case insensitive
    return strpos($needle, $haystack) !== false;
  });

  return empty($matches) ? false : $matches;
}

$namesArray = ['Peter', 'Glenn', 'Meg', 'Griffin'];

if(my_array_search($namesArray, 'Glenn Quagmire')){
   echo 'There is.'; // Glenn
} else {
   echo 'There is not.';
}
INFO