{"id":182183,"date":"2024-05-08T21:14:55","date_gmt":"2024-05-08T13:14:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/182183.html"},"modified":"2024-05-08T21:15:20","modified_gmt":"2024-05-08T13:15:20","slug":"%e5%a6%82%e4%bd%95%e5%af%b9php%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%a0%b9%e6%8d%ae%e7%ac%ac%e4%ba%8c%e7%bb%b4%e6%9f%90%e9%94%ae%e5%80%bc%e5%8e%bb%e9%87%8d%e5%92%8c%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/182183.html","title":{"rendered":"\u5982\u4f55\u5bf9PHP\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u53bb\u91cd\u548c\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27072151\/656e3512-28e4-45c0-9e73-00d0380834d0.webp\" alt=\"\u5982\u4f55\u5bf9PHP\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u53bb\u91cd\u548c\u6392\u5e8f\" \/><\/p>\n<p><p>\u5bf9PHP\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u7684\u67d0\u4e2a\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u548c\u6392\u5e8f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u64cd\u4f5c\u9700\u6c42\u3002<strong>\u5177\u4f53\u5230\u5982\u4f55\u5b9e\u73b0\uff0c\u4e3b\u8981\u6d89\u53ca\u5230\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u6570\u7ec4\u53bb\u91cd\u3001\u6570\u7ec4\u6392\u5e8f\u3001\u4ee5\u53ca\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406<\/strong>\u3002\u5728\u6570\u7ec4\u53bb\u91cd\u65b9\u9762\uff0c\u53ef\u4ee5\u5229\u7528PHP\u7684array_unique\u51fd\u6570\uff0c\u4f46\u8fd9\u4e2a\u51fd\u6570\u9ed8\u8ba4\u53ea\u9002\u7528\u4e8e\u4e00\u7ef4\u6570\u7ec4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u7ed3\u5408array_map\u7b49\u51fd\u6570\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u518d\u6267\u884c\u53bb\u91cd\u64cd\u4f5c\u3002\u800c\u5728\u6570\u7ec4\u6392\u5e8f\u65b9\u9762\uff0c\u53ef\u4ee5\u4f7f\u7528usort\u51fd\u6570\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff0c\u6839\u636e\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u6307\u5b9a\u952e\u503c\u7684\u987a\u5e8f\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u6570\u7ec4\u53bb\u91cd<\/strong><\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8ePHP\u4e8c\u7ef4\u6570\u7ec4\u7684\u53bb\u91cd\uff0c\u4e3b\u8981\u7684\u6311\u6218\u5728\u4e8e\u5982\u4f55\u5728\u4e0d\u4e22\u5931\u6570\u636e\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\uff0c\u9488\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u952e\u503c\u5b9e\u73b0\u53bb\u91cd\u64cd\u4f5c\u3002\u6709\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u662f\u9996\u5148\u901a\u8fc7array_map\u7ed3\u5408array_column\u51fd\u6570\u63d0\u53d6\u9700\u8981\u53bb\u91cd\u7684\u952e\u503c\uff0c\u7136\u540e\u4f7f\u7528array_unique\u51fd\u6570\u5bf9\u8fd9\u4e9b\u952e\u503c\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7528array_column\u51fd\u6570\u4ece\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u63d0\u53d6\u51fa\u9700\u8981\u53bb\u91cd\u4f9d\u636e\u7684\u90a3\u4e00\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$records = [<\/p>\n<p>    [&#039;id&#039; =&gt; 1, &#039;name&#039; =&gt; &#039;Alice&#039;],<\/p>\n<p>    [&#039;id&#039; =&gt; 2, &#039;name&#039; =&gt; &#039;Bob&#039;],<\/p>\n<p>    [&#039;id&#039; =&gt; 1, &#039;name&#039; =&gt; &#039;Alice&#039;],<\/p>\n<p>];<\/p>\n<p>$uniqueKeys = array_unique(array_column($records, &#039;id&#039;));<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7array_filter\u51fd\u6570\u6765\u8fc7\u6ee4\u6389\u90a3\u4e9b\u5728$uniqueKeys\u4e2d\u4e0d\u552f\u4e00\u7684\u6570\u7ec4\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$uniqueRecords = array_filter($records, function ($key) use ($uniqueKeys) {<\/p>\n<p>    return in_array($key[&#039;id&#039;], $uniqueKeys);<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u6570\u7ec4\u6392\u5e8f<\/strong><\/h3>\n<\/p>\n<p><p>\u6392\u5e8f\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u7ec4\u7ec7\u7684\u57fa\u672c\u65b9\u5f0f\u4e4b\u4e00\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u4e8c\u7ef4\u6570\u7ec4\u65f6\uff0c\u6839\u636e\u7b2c\u4e8c\u7ef4\u7684\u67d0\u4e2a\u952e\u503c\u8fdb\u884c\u6392\u5e8f\u662f\u5e38\u89c1\u9700\u6c42\u3002\u5728PHP\u4e2d\uff0c<strong>usort\u51fd\u6570\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u81ea\u5df1\u7684\u6bd4\u8f83\u51fd\u6570\u6765\u51b3\u5b9a\u5982\u4f55\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">usort($records, function($a, $b) {<\/p>\n<p>    return $a[&#039;name&#039;] &lt; $b[&#039;name&#039;] ? -1 : 1;<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u6839\u636ename\u952e\u503c\u5bf9$records\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002\u5982\u679c\u9700\u8981\u964d\u5e8f\u6392\u5e8f\uff0c\u53ea\u9700\u8c03\u6574\u6bd4\u8f83\u51fd\u6570\u4e2d\u7684\u8fd4\u56de\u503c\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u7ed3\u5408\u53bb\u91cd\u4e0e\u6392\u5e8f<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5c06\u53bb\u91cd\u548c\u6392\u5e8f\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$records = array_unique(array_map(&quot;serialize&quot;, $records));<\/p>\n<p>usort($records, function($a, $b) {<\/p>\n<p>    return $a[&#039;id&#039;] - $b[&#039;id&#039;];<\/p>\n<p>});<\/p>\n<p>$records = array_map(&quot;unserialize&quot;, $records);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9996\u5148\u4f7f\u7528serialize\u51fd\u6570\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5e8f\u5217\u5316\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u6267\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u968f\u540e\u53ef\u4ee5\u8c03\u7528usort\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u901a\u8fc7unserialize\u51fd\u6570\u5c06\u7ed3\u679c\u53cd\u5e8f\u5217\u5316\u56de\u539f\u6765\u7684\u4e8c\u7ef4\u6570\u7ec4\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u9ad8\u7ea7\u6570\u636e\u5904\u7406<\/strong><\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\uff0c\u4f8b\u5982\u7efc\u5408\u8003\u8651\u591a\u4e2a\u952e\u503c\u7684\u4f18\u5148\u7ea7\u8fdb\u884c\u6392\u5e8f\uff0c\u6216\u8005\u5bf9\u53bb\u91cd\u540e\u7684\u6570\u7ec4\u6309\u7167\u7279\u5b9a\u89c4\u5219\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5904\u7406\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7f16\u5199\u66f4\u52a0\u590d\u6742\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u5148\u6839\u636eid\u5347\u5e8f\u6392\u5e8f\uff0c\u82e5id\u76f8\u540c\u5219\u6309\u7167name\u964d\u5e8f\u6392\u5e8f\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u5728usort\u7684\u6bd4\u8f83\u51fd\u6570\u4e2d\u52a0\u5165\u66f4\u591a\u7684\u903b\u8f91\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">usort($records, function($a, $b) {<\/p>\n<p>    if ($a[&#039;id&#039;] == $b[&#039;id&#039;]) {<\/p>\n<p>        return $a[&#039;name&#039;] &lt; $b[&#039;name&#039;] ? 1 : -1;<\/p>\n<p>    }<\/p>\n<p>    return $a[&#039;id&#039;] &lt; $b[&#039;id&#039;] ? -1 : 1;<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u5728\u5904\u7406\u4e8c\u7ef4\u6570\u7ec4\u7684\u53bb\u91cd\u548c\u6392\u5e8f\u65f6\uff0cPHP\u63d0\u4f9b\u4e86\u591a\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\u548c\u51fd\u6570\u3002\u5173\u952e\u5728\u4e8e\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5de5\u5177\uff0c\u6709\u65f6\u5019\u53ef\u80fd\u9700\u8981\u7ec4\u5408\u4f7f\u7528\u591a\u79cd\u51fd\u6570\u3002\u6df1\u5165\u7406\u89e3\u8fd9\u4e9b\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q\uff1a\u5982\u4f55\u4f7f\u7528PHP\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>A\uff1a\u8981\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528PHP\u4e2d\u7684array_unique\u51fd\u6570\u7ed3\u5408\u81ea\u5b9a\u4e49\u6392\u5e8f\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528array_map\u51fd\u6570\u83b7\u53d6\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7b2c\u4e8c\u7ef4\u6307\u5b9a\u952e\u503c\u7684\u6570\u7ec4\uff0c\u5e76\u4e0e\u539f\u6570\u7ec4\u8fdb\u884c\u5408\u5e76\uff1b\u7136\u540e\uff0c\u5229\u7528array_unique\u51fd\u6570\u5bf9\u5408\u5e76\u540e\u7684\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u6700\u540e\u518d\u5229\u7528usort\u51fd\u6570\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u6837\u5c31\u80fd\u591f\u5b9e\u73b0\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u548c\u6392\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>Q\uff1a\u5982\u4f55\u4f7f\u7528PHP\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u6392\u5e8f\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>A\uff1a\u8981\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u6392\u5e8f\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528PHP\u4e2d\u7684usort\u51fd\u6570\u7ed3\u5408\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6392\u5e8f\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u53c2\u6570\u7684\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u7684\u5927\u5c0f\uff1b\u7136\u540e\uff0c\u5229\u7528usort\u51fd\u6570\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6392\u5e8f\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9usort\u51fd\u6570\uff0c\u8fd9\u6837\u5c31\u80fd\u591f\u5b9e\u73b0\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u6392\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>Q\uff1a\u5982\u4f55\u4f7f\u7528PHP\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u548c\u6392\u5e8f\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>A\uff1a\u8981\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u548c\u6392\u5e8f\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528array_unique\u51fd\u6570\u548cusort\u51fd\u6570\u3002\u9996\u5148\uff0c\u4f7f\u7528array_unique\u51fd\u6570\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u5f97\u5230\u53bb\u91cd\u540e\u7684\u6570\u7ec4\uff1b\u7136\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6392\u5e8f\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u6bd4\u8f83\uff1b\u6700\u540e\uff0c\u5229\u7528usort\u51fd\u6570\u5bf9\u53bb\u91cd\u540e\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6392\u5e8f\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9usort\u51fd\u6570\u3002\u8fd9\u6837\u5c31\u80fd\u591f\u5b9e\u73b0\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u67d0\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u548c\u6392\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5bf9PHP\u4e8c\u7ef4\u6570\u7ec4\u6839\u636e\u7b2c\u4e8c\u7ef4\u7684\u67d0\u4e2a\u952e\u503c\u8fdb\u884c\u53bb\u91cd\u548c\u6392\u5e8f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u64cd\u4f5c\u9700\u6c42\u3002\u5177\u4f53\u5230\u5982\u4f55\u5b9e\u73b0\uff0c\u4e3b\u8981\u6d89\u53ca\u5230\u4ee5\u4e0b\u51e0\u4e2a [&hellip;]","protected":false},"author":3,"featured_media":182206,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/182183"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=182183"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/182183\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/182206"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=182183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=182183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=182183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}