{"id":1086,"date":"2021-04-13T02:18:25","date_gmt":"2021-04-13T02:18:25","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1086"},"modified":"2025-04-06T03:47:40","modified_gmt":"2025-04-06T03:47:40","slug":"php-in_array","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-in_array\/","title":{"rendered":"PHP in_array"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the PHP <code>in_array()<\/code> function to check if a value exists in an array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-in_array-function'>Introduction to the PHP in_array() function <a href=\"#introduction-to-the-php-in_array-function\" class=\"anchor\" id=\"introduction-to-the-php-in_array-function\" title=\"Anchor for Introduction to the PHP in_array() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>in_array()<\/code> function returns <code>true<\/code> if a value exists in an array. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>in_array()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">in_array ( mixed $needle , <span class=\"hljs-keyword\">array<\/span> $haystack , bool $strict = <span class=\"hljs-keyword\">false<\/span> ) : bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$needle<\/code> is the searched value.<\/li>\n\n\n\n<li><code>$haystack<\/code> is the array to search.<\/li>\n\n\n\n<li><code>$strict<\/code> if the <code>$strict<\/code> sets to <code>true<\/code>, the <code>in_array()<\/code> function will use the <code>strict<\/code> comparison.<\/li>\n<\/ul>\n\n\n\n<p>The <code>in_array()<\/code> function searches for the <code>$needle<\/code> in the <code>$haystack<\/code> using the loose comparison (<code>==<\/code>). To use the strict comparison (<code>===<\/code>), you need to set the <code>$strict<\/code> argument to <code>true<\/code>.<\/p>\n\n\n\n<p>If the value to check is a string, the <code>in_array()<\/code> function will search for it case-sensitively.<\/p>\n\n\n\n<p>The <code>in_array()<\/code> function returns <code>true<\/code> if the <code>$needle<\/code> exists in the <code>$array<\/code>; otherwise, it returns <code>false<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-in_array-function-examples'>PHP in_array() function examples <a href=\"#php-in_array-function-examples\" class=\"anchor\" id=\"php-in_array-function-examples\" title=\"Anchor for PHP in_array() function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>in_array()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-simple-php-in_array-function-examples'>1) Simple PHP in_array() function examples <a href=\"#1-simple-php-in_array-function-examples\" class=\"anchor\" id=\"1-simple-php-in_array-function-examples\" title=\"Anchor for 1) Simple PHP in_array() function examples\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>in_array()<\/code> function to check if the value <code>'update'<\/code> is in the <code>$actions<\/code> array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$actions = &#91;\n\t<span class=\"hljs-string\">'new'<\/span>,\n\t<span class=\"hljs-string\">'edit'<\/span>,\n\t<span class=\"hljs-string\">'update'<\/span>,\n\t<span class=\"hljs-string\">'view'<\/span>,\n\t<span class=\"hljs-string\">'delete'<\/span>,\n];\n\n$result = in_array(<span class=\"hljs-string\">'update'<\/span>, $actions);\n\nvar_dump($result); <span class=\"hljs-comment\">\/\/ bool(true)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRhY3Rpb25zID0gWwoJJ25ldycsCgknZWRpdCcsCgkndXBkYXRlJywKCSd2aWV3JywKCSdkZWxldGUnLApdOwoKJHJlc3VsdCA9IGluX2FycmF5KCd1cGRhdGUnLCAkYWN0aW9ucyk7Cgp2YXJfZHVtcCgkcmVzdWx0KTsgLy8gYm9vbCh0cnVlKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">bool(<span class=\"hljs-keyword\">true<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returns <code>true<\/code>.<\/p>\n\n\n\n<p>The following example returns <code>false<\/code> because the <code>publish<\/code> value doesn&#8217;t exist in the <code>$actions<\/code> array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$actions = &#91;\n\t<span class=\"hljs-string\">'new'<\/span>,\n\t<span class=\"hljs-string\">'edit'<\/span>,\n\t<span class=\"hljs-string\">'update'<\/span>,\n\t<span class=\"hljs-string\">'view'<\/span>,\n\t<span class=\"hljs-string\">'delete'<\/span>,\n];\n\n$result = in_array(<span class=\"hljs-string\">'publish'<\/span>, $actions);\n\nvar_dump($result); <span class=\"hljs-comment\">\/\/ bool(false)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRhY3Rpb25zID0gWwoJJ25ldycsCgknZWRpdCcsCgkndXBkYXRlJywKCSd2aWV3JywKCSdkZWxldGUnLApdOwoKJHJlc3VsdCA9IGluX2FycmF5KCdwdWJsaXNoJywgJGFjdGlvbnMpOwoKdmFyX2R1bXAoJHJlc3VsdCk7IC8vIGJvb2woZmFsc2Up\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The following example returns <code>false<\/code> because the value <code>'New'<\/code> doesn&#8217;t exist in the <code>$actions<\/code> array. Note that the <code>in_array()<\/code> compares the strings case-sensitively:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$actions = &#91;\n\t<span class=\"hljs-string\">'new'<\/span>,\n\t<span class=\"hljs-string\">'edit'<\/span>,\n\t<span class=\"hljs-string\">'update'<\/span>,\n\t<span class=\"hljs-string\">'view'<\/span>,\n\t<span class=\"hljs-string\">'delete'<\/span>,\n];\n\n$result = in_array(<span class=\"hljs-string\">'New'<\/span>, $actions);\n\nvar_dump($result); <span class=\"hljs-comment\">\/\/ bool(false)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRhY3Rpb25zID0gWwoJJ25ldycsCgknZWRpdCcsCgkndXBkYXRlJywKCSd2aWV3JywKCSdkZWxldGUnLApdOwoKJHJlc3VsdCA9IGluX2FycmF5KCdOZXcnLCAkYWN0aW9ucyk7Cgp2YXJfZHVtcCgkcmVzdWx0KTsgLy8gYm9vbChmYWxzZSk\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">bool(<span class=\"hljs-keyword\">false<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-using-php-in_array-function-with-the-strict-comparison-example'>2) Using PHP in_array() function with the strict comparison example <a href=\"#2-using-php-in_array-function-with-the-strict-comparison-example\" class=\"anchor\" id=\"2-using-php-in_array-function-with-the-strict-comparison-example\" title=\"Anchor for 2) Using PHP in_array() function with the strict comparison example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>in_array()<\/code> function to find the number <code>15<\/code> in the <code>$user_ids<\/code> array. It returns <code>true<\/code> because the <code>in_array()<\/code> function compares the values using the loose comparison (<code>==<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$user_ids = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-string\">'15'<\/span>, <span class=\"hljs-string\">'20'<\/span>, <span class=\"hljs-number\">30<\/span>];\n\n$result = in_array(<span class=\"hljs-number\">15<\/span>, $user_ids);\n\nvar_dump($result); <span class=\"hljs-comment\">\/\/  bool(true)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiR1c2VyX2lkcyA9IFsxMCwgJzE1JywgJzIwJywgMzBdOwoKJHJlc3VsdCA9IGluX2FycmF5KDE1LCAkdXNlcl9pZHMpOwoKdmFyX2R1bXAoJHJlc3VsdCk7IC8vICBib29sKHRydWUp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>To use the strict comparison, you pass <code>true<\/code> to the third argument (<code>$strict<\/code>) of the <code>in_array()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$user_ids = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-string\">'15'<\/span>, <span class=\"hljs-string\">'20'<\/span>, <span class=\"hljs-number\">30<\/span>];\n\n$result = in_array(<span class=\"hljs-number\">15<\/span>, $user_ids, <span class=\"hljs-keyword\">true<\/span>);\n\nvar_dump($result); <span class=\"hljs-comment\">\/\/  bool(false)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This time the <code>in_array()<\/code> function returns <code>false<\/code> instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-the-php-in_array-function-with-the-searched-value-is-an-array-example'>Using the PHP in_array() function with the searched value is an array example <a href=\"#using-the-php-in_array-function-with-the-searched-value-is-an-array-example\" class=\"anchor\" id=\"using-the-php-in_array-function-with-the-searched-value-is-an-array-example\" title=\"Anchor for Using the PHP in_array() function with the searched value is an array example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>in_array()<\/code> function with the searched value is an array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$colors = &#91;\n\t&#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>],\n\t&#91;<span class=\"hljs-string\">'cyan'<\/span>, <span class=\"hljs-string\">'magenta'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'black'<\/span>],\n\t&#91;<span class=\"hljs-string\">'hue'<\/span>, <span class=\"hljs-string\">'saturation'<\/span>, <span class=\"hljs-string\">'lightness'<\/span>]\n];\n\n<span class=\"hljs-keyword\">if<\/span> (in_array(&#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>], $colors)) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'RGB colors found'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'RGB colors are not found'<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjb2xvcnMgPSBbCglbJ3JlZCcsICdncmVlbicsICdibHVlJ10sCglbJ2N5YW4nLCAnbWFnZW50YScsICd5ZWxsb3cnLCAnYmxhY2snXSwKCVsnaHVlJywgJ3NhdHVyYXRpb24nLCAnbGlnaHRuZXNzJ10KXTsKCmlmIChpbl9hcnJheShbJ3JlZCcsICdncmVlbicsICdibHVlJ10sICRjb2xvcnMpKSB7CgllY2hvICdSR0IgY29sb3JzIGZvdW5kJzsKfSBlbHNlIHsKCWVjaG8gJ1JHQiBjb2xvcnMgYXJlIG5vdCBmb3VuZCc7Cn0\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">RGB colors found<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='4-using-php-in_array-function-with-an-array-of-objects-example'>4) Using PHP in_array() function with an array of objects example <a href=\"#4-using-php-in_array-function-with-an-array-of-objects-example\" class=\"anchor\" id=\"4-using-php-in_array-function-with-an-array-of-objects-example\" title=\"Anchor for 4) Using PHP in_array() function with an array of objects example\">#<\/a><\/h3>\n\n\n\n<p>The following defines the <code>Role<\/code> class that has two properties <code>$id<\/code> and <code>$name<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Role<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $id;\n\n\t<span class=\"hljs-keyword\">private<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($id, $name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;id = $id;\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example illustrates how to use the <code>in_array()<\/code> function to check if a <code>Role<\/code> object exists in an array of <code>Role<\/code> objects:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Role<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $id;\n\n\t<span class=\"hljs-keyword\">private<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($id, $name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;id = $id;\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t}\n}\n\n$roles = &#91;\n\t<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'admin'<\/span>),\n\t<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'editor'<\/span>),\n\t<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">'subscribe'<\/span>),\n];\n\n<span class=\"hljs-keyword\">if<\/span> (in_array(<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'admin'<\/span>), $roles)) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'found it'<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIFJvbGUKewoJcHJpdmF0ZSAkaWQ7CgoJcHJpdmF0ZSAkbmFtZTsKCglwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3QoJGlkLCAkbmFtZSkKCXsKCQkkdGhpcy0-aWQgPSAkaWQ7CgkJJHRoaXMtPm5hbWUgPSAkbmFtZTsKCX0KfQoKJHJvbGVzID0gWwoJbmV3IFJvbGUoMSwgJ2FkbWluJyksCgluZXcgUm9sZSgyLCAnZWRpdG9yJyksCgluZXcgUm9sZSgzLCAnc3Vic2NyaWJlJyksCl07CgppZiAoaW5fYXJyYXkobmV3IFJvbGUoMSwgJ2FkbWluJyksICRyb2xlcykpIHsKCWVjaG8gJ2ZvdW5kIGl0JzsKfQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">found it!<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you set the <code>$strict<\/code> to <code>true<\/code>, the <code>in_array()<\/code> function will compare objects using their identities instead of values. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Role<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $id;\n\n\t<span class=\"hljs-keyword\">private<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($id, $name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;id = $id;\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t}\n}\n\n$roles = &#91;\n\t<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'admin'<\/span>),\n\t<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'editor'<\/span>),\n\t<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">'subscribe'<\/span>),\n];\n\n<span class=\"hljs-keyword\">if<\/span> (in_array(<span class=\"hljs-keyword\">new<\/span> Role(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'admin'<\/span>), $roles, <span class=\"hljs-keyword\">true<\/span>)) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'found it!'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'not found!'<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIFJvbGUKewoJcHJpdmF0ZSAkaWQ7CgoJcHJpdmF0ZSAkbmFtZTsKCglwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3QoJGlkLCAkbmFtZSkKCXsKCQkkdGhpcy0-aWQgPSAkaWQ7CgkJJHRoaXMtPm5hbWUgPSAkbmFtZTsKCX0KfQoKJHJvbGVzID0gWwoJbmV3IFJvbGUoMSwgJ2FkbWluJyksCgluZXcgUm9sZSgyLCAnZWRpdG9yJyksCgluZXcgUm9sZSgzLCAnc3Vic2NyaWJlJyksCl07CgppZiAoaW5fYXJyYXkobmV3IFJvbGUoMSwgJ2FkbWluJyksICRyb2xlcywgdHJ1ZSkpIHsKCWVjaG8gJ2ZvdW5kIGl0ISc7Cn0gZWxzZSB7CgllY2hvICdub3QgZm91bmQhJzsKfQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">not found!<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use PHP <code>in_array()<\/code> function to check if a value exists in an array.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1086\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-in_array\/\"\n\t\t\t\tdata-post-title=\"PHP in_array\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1086\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-in_array\/\"\n\t\t\t\tdata-post-title=\"PHP in_array\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s the syntax of the in_array() function: In this syntax: The in_array() function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":51,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1086","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1086"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1086\/revisions"}],"predecessor-version":[{"id":3072,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1086\/revisions\/3072"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}