How to check if an array value exists?

Summary: in this tutorial, you will learn how to lớn use the PHP in_array() function to kiểm tra if a value exists in an array.

Bạn đang xem: How to check if an array value exists?

Introduction lớn the PHP in_array() function

The in_array() function returns true if a value exists in an array. Here’s the syntax of the in_array() function:


Code language: PHP (php)In this syntax:

$needle is the searched value.$haystack is the array lớn search.$strict if the $strict sets to lớn true, the in_array() function will use the strict comparison.

The in_array() function searches for the $needle in the $haystack using the loose comparison (==). To lớn use the strict comparison (===), you need to lớn set the $strict argument to true.

If the value to check is a string, the in_array() function will tìm kiếm for it case-sensitively.

The in_array() function returns true if the $needle exists in the $array; otherwise, it returns false.

Xem thêm: Phần Mềm Remove.Bg - Remove Image Backgrounds Automatically

PHP in_array() function examples

Let’s take some examples of using the in_array() function.

1) Simple PHP in_array() function examples

The following example uses the in_array() function to kiểm tra if the value "update" is in the $actions array:


$actions = <"new","edit","update","view","delete",>;$result = in_array("update", $actions);var_dump($result); // bool(true)
Code language: HTML, XML (xml)It returns true.

The following example returns false because the publish value doesn’t exist in the $actions array:


$actions = <"new","edit","update","view","delete",>;$result = in_array("publish", $actions);var_dump($result); // bool(false)
Code language: HTML, XML (xml)The following example returns false because the value "New" doesn’t exist in the $actions array. Lưu ý that the in_array() compares the strings case-sensitively:


$actions = <"new","edit","update","view","delete",>;$result = in_array("New", $actions);var_dump($result); // bool(false)
Code language: HTML, XML (xml)

2) Using PHP in_array() function with the strict comparison example

The following example uses the in_array() function lớn find the number 15 in the $user_ids array. It returns true because the in_array() function compares the values using the loose comparison (==):


$user_ids = <10, "15", "20", 30>;$result = in_array(15, $user_ids);var_dump($result); // bool(true)
Code language: HTML, XML (xml)To use the strict comparison, you pass false to lớn the third argument ($strict) of the in_array() function as follows:


$user_ids = <10, "15", "20", 30>;$result = in_array(15, $user_ids, true);var_dump($result); // bool(false)
Code language: HTML, XML (xml)This time the in_array() function returns false instead.

3) Using PHP in_array() function with the searched value is an array example

The following example uses the in_array() function with the searched value is an array:


$colors = <<"red", "green", "blue">,<"cyan", "magenta", "yellow", "black">,<"hue", "saturation", "lightness">>;if (in_array(<"red", "green", "blue">, $colors)) echo "RGB colors found"; else echo "RGB colors are not found";
Code language: HTML, XML (xml)Output:


RGB colors found

4) Using PHP in_array() function with an array of objects example

The following defines the Role class that has two properties $id & $name:


class Roleprivate $id;private $name;public function __construct($id, $name)$this->id = $id;$this->name = $name;
Code language: HTML, XML (xml)This example illustrates how to lớn use the in_array() function to kiểm tra if a Role object exists in an array of Role objects:


// Role class$roles = ;if (in_array(new Role(1, "admin"), $roles)) echo "found it";
Code language: HTML, XML (xml)Output:


found it!
If you phối the $strict to lớn true, the in_array() function will compare objects using their identities instead of values. For example:


// Role class$roles = ;if (in_array(new Role(1, "admin"), $roles, true)) echo "found it!"; else echo "not found!";
Code language: PHP (php)Output:


not found!

Summary

Use PHP in_array() function to check if a value exists in an array.
Did you find this tutorial useful?
Yes No
*

Previously
PHP array_key_exists
Up Next
PHP array_reverse
*

tìm kiếm for:

Getting Started


PHP Fundamentals


Data Types


Operators


Control Flow


Functions


Arrays


Sorting Arrays


Advanced Functions


Advanced Array Operations


Forms


PHP Login


File I/O


String Opearations


About obatambeienwasirherbal.com


obatambeienwasirherbal.com helps you learn PHP programming from scratch.

Recent PHP Tutorials


Site Links