{"id":2813,"date":"2025-08-29T06:12:04","date_gmt":"2025-08-28T22:12:04","guid":{"rendered":"https:\/\/www.ddosidc.com\/wp\/?p=2813"},"modified":"2025-08-29T06:12:04","modified_gmt":"2025-08-28T22:12:04","slug":"%e6%8e%8c%e6%8f%a1php-in_array-%e7%94%a8%e6%b3%95%ef%bc%9a%e9%ab%98%e6%95%88%e6%a3%80%e6%9f%a5%e6%95%b0%e7%bb%84%e5%85%83%e7%b4%a0%e5%ad%98%e5%9c%a8%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.ddosidc.com\/wp\/2813.html","title":{"rendered":"\u638c\u63e1PHP in_array \u7528\u6cd5\uff1a\u9ad8\u6548\u68c0\u67e5\u6570\u7ec4\u5143\u7d20\u5b58\u5728\u6027"},"content":{"rendered":"<p><h2>PHP in_array \u7528\u6cd5<\/h2>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.ddosidc.com\/wp\/wp-content\/uploads\/2025\/05\/Dc03WI.jpg\" alt=\"\u638c\u63e1PHP in_array \u7528\u6cd5\uff1a\u9ad8\u6548\u68c0\u67e5\u6570\u7ec4\u5143\u7d20\u5b58\u5728\u6027\" title=\"\u638c\u63e1PHP in_array \u7528\u6cd5\uff1a\u9ad8\u6548\u68c0\u67e5\u6570\u7ec4\u5143\u7d20\u5b58\u5728\u6027\"><\/p>\n<p><p>\u5728PHP\u4e2d\uff0c<strong>in_array<\/strong>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u67d0\u4e2a\u7279\u5b9a\u503c\u662f\u5426\u88ab\u5305\u542b\u5728\u7ed9\u5b9a\u6570\u7ec4\u4e2d\uff0c\u4ece\u800c\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u548c\u6570\u636e\u9a8c\u8bc1\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8<code>in_array<\/code>\u51fd\u6570\u7684\u7528\u6cd5\uff0c\u5305\u62ec\u8be6\u7ec6\u7684\u64cd\u4f5c\u6b65\u9aa4\u548c\u793a\u4f8b\uff0c\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u548c\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>1. in_array \u51fd\u6570\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p><strong>in_array<\/strong>\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>bool in_array ( mixed $needle , array $haystack [, bool $strict = false ] )<\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>$needle<\/strong>\uff1a\u8981\u641c\u7d22\u7684\u503c\u3002<\/li>\n<li><strong>$haystack<\/strong>\uff1a\u8981\u641c\u7d22\u7684\u6570\u7ec4\u3002<\/li>\n<li><strong>$strict<\/strong>\uff08\u53ef\u9009\uff09\uff1a\u5982\u679c\u4e3a true\uff0c\u5219\u5728\u6bd4\u8f83\u65f6\u4f1a\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002<\/li>\n<\/ul>\n<p><p>\u5982\u679c<code>needle<\/code>\u5728<code>haystack<\/code>\u6570\u7ec4\u4e2d\uff0c\u5219\u8fd4\u56de<strong>true<\/strong>\uff0c\u5426\u5219\u8fd4\u56de<strong>false<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>2. \u57fa\u672c\u7528\u6cd5\u793a\u4f8b<\/h3>\n<\/p>\n<p><h4>2.1 \u68c0\u67e5\u503c\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>in_array<\/code>\u51fd\u6570\u68c0\u67e5\u67d0\u4e2a\u503c\u662f\u5426\u5728\u6570\u7ec4\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code>&lt;?php<\/p>\r\n<p>$fruits = array(\"apple\", \"banana\", \"orange\");<\/p>\r\n\r\n<p>if (in_array(\"banana\", $fruits)) {<\/p>\r\n<p>    echo \"\u9999\u8549\u5728\u6570\u7ec4\u4e2d\u3002\";<\/p>\r\n<p>} else {<\/p>\r\n<p>    echo \"\u9999\u8549\u4e0d\u5728\u6570\u7ec4\u4e2d\u3002\";<\/p>\r\n<p>}<\/p>\r\n<p>?&gt;<\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u6bb5\u4f1a\u8f93\u51fa<strong>\u201c\u9999\u8549\u5728\u6570\u7ec4\u4e2d\u3002\u201d<\/strong>\uff0c\u56e0\u4e3a\u201cbanana\u201d\u786e\u5b9e\u5b58\u5728\u4e8e$fruits\u6570\u7ec4\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528\u4e25\u683c\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002\u4f8b\u5982\uff0c\u6570\u5b570\u548c\u5b57\u7b26\u4e32&#8221;0&#8243;\u770b\u8d77\u6765\u76f8\u7b49\uff0c\u4f46\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u5b83\u4eec\u4f1a\u88ab\u89c6\u4e3a\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><pre><code>&lt;?php<\/p>\r\n<p>$numbers = array(1, 2, 3, \"0\");<\/p>\r\n\r\n<p>if (in_array(0, $numbers, true)) {<\/p>\r\n<p>    echo \"0\u5728\u6570\u7ec4\u4e2d\u3002\";<\/p>\r\n<p>} else {<\/p>\r\n<p>    echo \"0\u4e0d\u5728\u6570\u7ec4\u4e2d\u3002\";<\/p>\r\n<p>}<\/p>\r\n<p>?&gt;<\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u793a\u4f8b\u4f1a\u8f93\u51fa<strong>\u201c0\u4e0d\u5728\u6570\u7ec4\u4e2d\u3002\u201d<\/strong>\uff0c\u56e0\u4e3a\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c0\u4e0e&#8221;0&#8243;\u88ab\u89c6\u4e3a\u4e0d\u540c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>3. \u7ed3\u5408\u6570\u7ec4\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>3.1 \u68c0\u67e5\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06<code>in_array<\/code>\u4e0e\u5faa\u73af\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u68c0\u67e5\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code>&lt;?php<\/p>\r\n<p>$array = array(<\/p>\r\n<p>    array(\"id\" =&gt; 1, \"name\" =&gt; \"Alice\"),<\/p>\r\n<p>    array(\"id\" =&gt; 2, \"name\" =&gt; \"Bob\"),<\/p>\r\n<p>    array(\"id\" =&gt; 3, \"name\" =&gt; \"Charlie\")<\/p>\r\n<p>);<\/p>\r\n\r\n<p>$found = false;<\/p>\r\n<p>foreach ($array as $item) {<\/p>\r\n<p>    if (in_array(\"Bob\", $item)) {<\/p>\r\n<p>        $found = true;<\/p>\r\n<p>        break;<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n\r\n<p>if ($found) {<\/p>\r\n<p>    echo \"\u627e\u5230\u4e86Bob\uff01\";<\/p>\r\n<p>} else {<\/p>\r\n<p>    echo \"\u6ca1\u6709\u627e\u5230Bob\u3002\";<\/p>\r\n<p>}<\/p>\r\n<p>?&gt;<\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5faa\u73af\u904d\u5386\u591a\u7ef4\u6570\u7ec4\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5b50\u6570\u7ec4\u4e2d\u662f\u5426\u5305\u542b\u201cBob\u201d\u3002\u6700\u7ec8\u4f1a\u8f93\u51fa<strong>\u201c\u627e\u5230\u4e86Bob\uff01\u201d<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>4. \u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u6027\u80fd\uff1a<\/strong>\u5bf9\u4e8e\u5927\u578b\u6570\u7ec4\uff0c\u5982\u679c\u9700\u8981\u9891\u7e41\u68c0\u67e5\u503c\u662f\u5426\u5b58\u5728\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u5efa\u8bae\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u5982\u96c6\u5408\u6216\u54c8\u5e0c\u8868\u3002<\/li>\n<li><strong>\u7c7b\u578b\u6bd4\u8f83\uff1a<\/strong>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>in_array<\/code>\u51fd\u6570\u4f1a\u8fdb\u884c\u975e\u4e25\u683c\u6bd4\u8f83\uff0c\u6ce8\u610f\u8fd9\u4e00\u70b9\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002<\/li>\n<li><strong>\u91cd\u590d\u503c\uff1a<\/strong>\u5982\u679c\u6570\u7ec4\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0c<code>in_array<\/code>\u51fd\u6570\u4ecd\u4f1a\u8fd4\u56de<strong>true<\/strong>\u3002<\/li>\n<\/ul>\n<p><h3>5. \u5b9e\u7528\u6280\u5de7<\/h3>\n<\/p>\n<ul>\n<li><strong>\u6570\u7ec4\u53bb\u91cd\uff1a<\/strong>\u5728\u4f7f\u7528<code>in_array<\/code>\u65f6\uff0c\u5982\u679c\u4f60\u9700\u8981\u68c0\u67e5\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528<code>array_unique<\/code>\u51fd\u6570\u53bb\u9664\u91cd\u590d\u9879\u3002<\/li>\n<p><pre><code>&lt;?php<\/p>\r\n<p>$arr = array(1, 2, 2, 3, 4);<\/p>\r\n<p>$arr_unique = array_unique($arr);<\/p>\r\n\r\n<p>if (in_array(2, $arr_unique)) {<\/p>\r\n<p>    echo \"2\u5728\u53bb\u91cd\u540e\u7684\u6570\u7ec4\u4e2d\u3002\";<\/p>\r\n<p>}<\/p>\r\n<p>?&gt;<\/code><\/pre>\n<\/li>\n<li><strong>\u7ed3\u5408\u5176\u4ed6\u6570\u7ec4\u51fd\u6570\uff1a<\/strong>\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>array_map<\/code>\u7b49\u5176\u4ed6\u51fd\u6570\u6765\u52a8\u6001\u751f\u6210\u9700\u8981\u68c0\u67e5\u7684\u6570\u7ec4\u3002<\/li>\n<p><pre><code>&lt;?php<\/p>\r\n<p>$users = array(<\/p>\r\n<p>    array(\"name\" =&gt; \"Alice\", \"age\" =&gt; 25),<\/p>\r\n<p>    array(\"name\" =&gt; \"Bob\", \"age\" =&gt; 30),<\/p>\r\n<p>    array(\"name\" =&gt; \"Charlie\", \"age\" =&gt; 35)<\/p>\r\n<p>);<\/p>\r\n\r\n<p>$user_names = array_map(function($user) {<\/p>\r\n<p>    return $user[\"name\"];<\/p>\r\n<p>}, $users);<\/p>\r\n\r\n<p>if (in_array(\"Alice\", $user_names)) {<\/p>\r\n<p>    echo \"\u627e\u5230\u4e86Alice\uff01\";<\/p>\r\n<p>}<\/p>\r\n<p>?&gt;<\/code><\/pre>\n<\/li>\n<li><strong>\u68c0\u6d4b\u591a\u4e2a\u503c\uff1a<\/strong>\u5982\u679c\u9700\u8981\u68c0\u6d4b\u591a\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/li>\n<p><pre><code>&lt;?php<\/p>\r\n<p>$values = array(\"apple\", \"orange\", \"banana\");<\/p>\r\n<p>$checks = array(\"banana\", \"kiwi\", \"grape\");<\/p>\r\n\r\n<p>foreach ($checks as $check) {<\/p>\r\n<p>    if (in_array($check, $values)) {<\/p>\r\n<p>        echo \"$check \u5728\u6570\u7ec4\u4e2d\u3002\";<\/p>\r\n<p>    } else {<\/p>\r\n<p>        echo \"$check \u4e0d\u5728\u6570\u7ec4\u4e2d\u3002\";<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n<p>?&gt;<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p><strong>in_array<\/strong>\u51fd\u6570\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u503c\u3002\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u3001\u9ad8\u6548\u5730\u4f7f\u7528<code>in_array<\/code>\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6PHP\u529f\u80fd\u6765\u63d0\u9ad8\u4f60\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528<code>in_array<\/code>\u51fd\u6570\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP in_array \u7528\u6cd5 \u5728PHP\u4e2d\uff0cin_array\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u67d0\u4e2a\u7279\u5b9a\u503c\u662f\u5426\u88ab\u5305\u542b\u5728\u7ed9\u5b9a\u6570\u7ec4\u4e2d\uff0c\u4ece\u800c\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u548c\u6570\u636e\u9a8c\u8bc1\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8in_array\u51fd\u6570\u7684\u7528&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2814,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-2813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-baike"],"_links":{"self":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/2813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/comments?post=2813"}],"version-history":[{"count":2,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/2813\/revisions"}],"predecessor-version":[{"id":2989,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/2813\/revisions\/2989"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/media\/2814"}],"wp:attachment":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/media?parent=2813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/categories?post=2813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/tags?post=2813"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/topic?post=2813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}