PHP in_array() Function (original) (raw)
Summary: in this tutorial, you will learn how to use the PHP in_array()
function to check if a value exists in an array.
Introduction to 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:
in_array ( mixed <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>e</mi><mi>e</mi><mi>d</mi><mi>l</mi><mi>e</mi><mo separator="true">,</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi></mrow><annotation encoding="application/x-tex">needle , array </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">n</span><span class="mord mathnormal">ee</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>haystack , bool $strict = false ) : bool
Code language: PHP (php)
In this syntax:
$needle
is the searched value.$haystack
is the array to search.$strict
if the$strict
sets totrue
, thein_array()
function will use thestrict
comparison.
The in_array()
function searches for the $needle
in the $haystack
using the loose comparison (==
). To use the strict comparison (===
), you need to set the $strict
argument to true
.
If the value to check is a string, the in_array()
function will search for it case-sensitively.
The in_array()
function returns true
if the $needle
exists in the $array
; otherwise, it returns false
.
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 check if the value 'update'
is in the $actions
array:
`<?php
$actions = [ 'new', 'edit', 'update', 'view', 'delete', ]; result=inarray(′update′,result = in_array('update', result=inarray(′update′,actions);
var_dump($result); // bool(true)`Code language: PHP (php)
Output:
bool(true)
Code language: PHP (php)
It returns true
.
The following example returns false
because the publish
value doesn’t exist in the $actions
array:
`<?php
$actions = [ 'new', 'edit', 'update', 'view', 'delete', ]; result=inarray(′publish′,result = in_array('publish', result=inarray(′publish′,actions);
var_dump($result); // bool(false)`Code language: PHP (php)
The following example returns false
because the value 'New'
doesn’t exist in the $actions
array. Note that the in_array()
compares the strings case-sensitively:
`<?php
$actions = [ 'new', 'edit', 'update', 'view', 'delete', ]; result=inarray(′New′,result = in_array('New', result=inarray(′New′,actions);
var_dump($result); // bool(false)`Code language: PHP (php)
Output:
bool(false)
Code language: PHP (php)
2) Using PHP in_array() function with the strict comparison example #
The following example uses the in_array()
function to find the number 15
in the $user_ids
array. It returns true
because the in_array()
function compares the values using the loose comparison (==
):
`<?php
$user_ids = [10, '15', '20', 30]; result=inarray(15,result = in_array(15, result=inarray(15,user_ids);
var_dump($result); // bool(true)`Code language: PHP (php)
To use the strict comparison, you pass true
to the third argument ($strict
) of the in_array()
function as follows:
`<?php
$user_ids = [10, '15', '20', 30]; result=inarray(15,result = in_array(15, result=inarray(15,user_ids, true);
var_dump($result); // bool(false)`Code language: PHP (php)
This time the in_array()
function returns false
instead.
Using the 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:
`<?php
$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: PHP (php)
Output:
RGB colors found
Code language: PHP (php)
4) Using PHP in_array() function with an array of objects example #
The following defines the Role
class that has two properties $id
and $name
:
`<?php
class Role { private $id;
private $name;
public function __construct($id, $name)
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>i</mi><mi>d</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->id = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>id;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->name = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>name;
}
}`Code language: PHP (php)
This example illustrates how to use the in_array()
function to check if a Role
object exists in an array of Role
objects:
`<?php
class Role { private $id;
private $name;
public function __construct($id, $name)
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>i</mi><mi>d</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->id = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>id;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->name = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>name;
}
}
$roles = [ new Role(1, 'admin'), new Role(2, 'editor'), new Role(3, 'subscribe'), ];
if (in_array(new Role(1, 'admin'), $roles)) { echo 'found it'; }`Code language: PHP (php)
Output:
found it!
Code language: PHP (php)
If you set the $strict
to true
, the in_array()
function will compare objects using their identities instead of values. For example:
`<?php
class Role { private $id;
private $name;
public function __construct($id, $name)
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>i</mi><mi>d</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->id = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>id;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>></mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this->name = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>name;
}
}
$roles = [ new Role(1, 'admin'), new Role(2, 'editor'), new Role(3, 'subscribe'), ];
if (in_array(new Role(1, 'admin'), $roles, true)) { echo 'found it!'; } else { echo 'not found!'; }`Code language: PHP (php)
Output:
not found!
Code language: PHP (php)
Summary #
- Use PHP
in_array()
function to check if a value exists in an array.
Did you find this tutorial useful?