In PHP, how do I search arrays with examples?

In PHP, searching arrays can be done using various built-in functions. Below are some common methods to find elements in arrays.

Example of Searching an Array

You can use the `in_array()` function to check if a value exists in an array:

<?php $fruits = array("apple", "banana", "cherry"); $searchValue = "banana"; if (in_array($searchValue, $fruits)) { echo "Found $searchValue in the array!"; } else { echo "$searchValue is not in the array."; } ?>

Alternatively, if you need to find the key of a specific value, you can use the `array_search()` function:

<?php $fruits = array("apple", "banana", "cherry"); $searchValue = "cherry"; $key = array_search($searchValue, $fruits); if ($key !== false) { echo "Found $searchValue at index $key!"; } else { echo "$searchValue is not in the array."; } ?>

PHP array search in_array array_search PHP examples array functions