{"id":418,"date":"2021-06-03T17:41:28","date_gmt":"2021-06-03T09:41:28","guid":{"rendered":"https:\/\/wp.dj47.top\/?p=418"},"modified":"2021-10-20T16:11:03","modified_gmt":"2021-10-20T08:11:03","slug":"php-in_array-%e5%88%a4%e6%96%ad%e9%97%ae%e9%a2%98%ef%bc%88%e5%9d%91%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=418","title":{"rendered":"PHP in_array \u5224\u65ad\u95ee\u9898\uff08\u5751\uff09"},"content":{"rendered":"<pre class=\"language-php\"><code>&lt;?php\r\n$arr = array(\"Linux\");\r\nif (in_array(0, $arr)) {\r\n    echo \"match\";\r\n}\r\n?&gt;<\/code><\/pre>\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c0\u548c\u5b57\u7b26\u4e32\u662f\u53ef\u4ee5\u5339\u914d\u6210\u529f\u7684\u3002<\/p>\n<p>\u539f\u56e0\u662f\u5728in_array\uff0c\u5982\u679c\u6bd4\u8f83\u7684\u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u5e76\u4e14\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f0\uff0c\u5b83\u4f1a\u8fd4\u56detrue\uff08\u4e0d\u6b63\u786e\uff09\u3002<\/p>\n<p>\u67e5\u624b\u518c\uff1aIf the third parameter strict is set to TRUE then the in_array() function will also check thetypes of theneedle in thehaystack.<\/p>\n<p>\u52a0\u4e0a\u7c7b\u578b\u6bd4\u8f83\u540e\u8fd4\u56defalse\uff08\u6b63\u786e\uff09<\/p>\n<p>\u7ecf\u67e5\u9605\u76f8\u5173\u8d44\u6599\u5982\u4e0b\uff1a<\/p>\n<p>1.\u60c5\u51b5\u4e00<\/p>\n<pre class=\"language-php\"><code>$test = 'a';\r\n$array = array('a', 'b', 'c');\r\nif (in_array($test, $array)) {\r\necho 'in array';\r\n} else {\r\n    echo 'no';\r\n}\r\n\/\/output: in array<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>2.\u60c5\u51b5\u4e8c<\/p>\n<pre class=\"language-php\"><code>$test = 'other value';\r\n$array = array('a', 'b', 'c');\r\nif (in_array($test, $array)) {\r\necho 'in array';\r\n} else {\r\n    echo 'no';\r\n}\r\n\/\/output: no<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>3.\u60c5\u51b5\u4e09<\/p>\n<pre class=\"language-php\"><code>$test = 0;\r\n$array = array('a', 'b', 'c');\r\nif (in_array($test, $array)) {\r\n    echo 'in array';\r\n} else {\r\n    echo 'no';\r\n}\r\n\/\/output: in array<\/code><\/pre>\n<p>\u96be\u4ee5\u7f6e\u4fe1\uff0c\u8fd9\u4e0d\u5751\u7239\u5417\uff0c0\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u4f46\u662f\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56detrue\u3002\u8fd9\u4e2a\u51fd\u6570\u5f88\u5e38\u7528\uff0c\u6240\u4ee5\u5f53\u67d0\u4e9b\u5de7\u5408\u51fa\u73b0\u540e\uff0c\u5c31\u4f1a\u5bfc\u81f4\u4e0d\u53ef\u9884\u6599\u7684\u9519\u8bef\u3002<\/p>\n<p>\u4e00\u5f00\u59cb\u6211\u731c\u6d4bin_array\u51fd\u6570\u9664\u4e86\u5728values\u4e2d\u5bfb\u627e\uff0c\u8fd8\u4f1a\u53bb\u5bfb\u627ekeys\uff0c\u540e\u6765\u53d1\u73b0\u4e0d\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u60c5\u51b5\u56db<\/p>\n<pre class=\"language-php\"><code>$test = '0';\r\n$array = array('a', 'b', 'c');\r\nif (in_array($test, $array)) {\r\n    echo 'in array';\r\n} else {\r\n    echo 'no';\r\n}\r\n\/\/output: no<\/code><\/pre>\n<p>\u662f\u4e0d\u662f\u5f88\u6709\u610f\u601d<\/p>\n<p>&nbsp;<\/p>\n<p>5.\u539f\u6765\u662f\u56e0\u4e3a\uff1a<a style=\"box-sizing: border-box; color: #075db3; text-decoration-line: none; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 0px; padding: 0px;\" href=\"http:\/\/www.voidcn.com\/link?url=http:\/\/www.php.net\/manual\/en\/language.operators.comparison.php\" target=\"_blank\" rel=\"nofollow noopener\">http:\/\/www.php.net\/manual\/en\/language.operators.comparison.php<\/a><\/p>\n<pre class=\"language-php\"><code>var_dump(0 == \"a\"); \/\/ 0 == 0 -&gt; true\r\nvar_dump(\"1\" == \"01\"); \/\/ 1 == 1 -&gt; true\r\nvar_dump(\"10\" == \"1e1\"); \/\/ 10 == 10 -&gt; true\r\nvar_dump(100 == \"1e2\"); \/\/ 100 == 100 -&gt; true<\/code><\/pre>\n<p>php\u6bd4\u8f83\u6570\u5b57\u548c\u5b57\u7b26\u4e32\u65f6\uff0c\u4f1a\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u518d\u8fdb\u884c\u6bd4\u8f83\u3002 \u4f8b\u5b50\u4e2d\u7684 &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217; \u8f6c\u6210\u6570\u5b57\u90fd\u662f0\uff0c\u6240\u4ee5\u548c0\u76f8\u7b49\uff0cin_array\u5c31\u8fd4\u56de\u4e86true\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>6.\u5982\u4f55\u907f\u514d<\/p>\n<p>PHP\u7684\u7c7b\u578b\u620f\u6cd5\u662f\u628a\u53cc\u5203\u5251\uff0c\u6709\u65f6\u5019\u5f88\u723d\uff0c\u6709\u65f6\u5019\u5f88\u8d31\u3002<\/p>\n<p>\u6240\u4ee5\u5f53in_array\u7684needle\u4e0earray\u4e2d\u7684\u503c\u7c7b\u578b\u662f\u5426\u76f8\u540c\u8fd8\u4e0d\u786e\u5b9a\u65f6\uff0c\u6700\u597d\u8bbe\u7f6ein_array\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570 strict = true\uff0c\u8fd9\u6837\u5728\u68c0\u67e5\u7684\u65f6\u5019\u4f1a\u68c0\u67e5\u7c7b\u578b\uff0c\u6570\u5b57\u4e0e\u5b57\u7b26\u4e32\u5c31\u4e0d\u4f1a\u5077\u5077\u76f8\u7b49\uff0c\u4e5f\u5c31\u907f\u514d\u7c7b\u4f3c\u8fd9\u79cd\u95ee\u9898\u3002<\/p>\n<pre class=\"language-php\"><code>$test = 0;\r\n$array = array('a', 'b', 'c');\r\nif (in_array($test, $array, true)) {\r\n    echo 'in array';\r\n \r\n} else {\r\n    echo 'no';\r\n}\r\n\/\/output: no<\/code><\/pre>\n<p>\u8f6c\u81ea\uff1ahttp:\/\/blog.csdn.net\/edwingu\/article\/details\/8876981<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php $arr = array(&#8220;Linux&#8221;); if (in_array(0, $arr))  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"class_list":["post-418","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=418"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/418\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}