In PHP, how do I search arrays with built-in functions?

In PHP, you can search arrays using various built-in functions. Some commonly used functions for searching include:

  • in_array() - Checks if a value exists in an array.
  • array_search() - Searches for a value in an array and returns the key.
  • array_filter() - Filters elements of an array using a callback function.

Here's an example of how to use these functions:

<?php $fruits = array("apple", "banana", "cherry", "date"); // Using in_array() if (in_array("banana", $fruits)) { echo "Banana is in the array!"; } // Using array_search() $key = array_search("cherry", $fruits); if ($key !== false) { echo "Cherry is found at index: " . $key; } // Using array_filter() $filtered = array_filter($fruits, function($fruit) { return strlen($fruit) > 5; }); print_r($filtered); ?>

PHP array search in_array array_search array_filter