Array Search on Key


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

like array_search but compares against a specific key in each element of the array


Copy this code and paste it in your HTML
  1. function array_search_key($needle, $haystack, $haystackKey, $strict = false) {
  2. foreach($haystack as $key => $value) {
  3. if(isset($value[$haystackKey])) {
  4. if($strict) {
  5. if($value[$haystackKey] === $needle) return true;
  6. } else {
  7. if($value[$haystackKey] == $needle) return true;
  8. }
  9. }
  10. }
  11.  
  12. return false;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.