{"id":2982,"date":"2025-03-19T00:04:17","date_gmt":"2025-03-18T16:04:17","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2982"},"modified":"2025-03-19T00:04:17","modified_gmt":"2025-03-18T16:04:17","slug":"php-%e6%95%b0%e7%bb%84%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-%e6%95%b0%e7%bb%84%e6%8e%92%e5%ba%8f\/","title":{"rendered":"PHP \u6570\u7ec4\u6392\u5e8f"},"content":{"rendered":"\n<p>\u5728 PHP \u4e2d\uff0c\u6570\u7ec4\u6392\u5e8f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0cPHP \u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u5305\u62ec\u6309\u503c\u6392\u5e8f\u3001\u6309\u952e\u6392\u5e8f\u4ee5\u53ca\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u6309\u503c\u6392\u5e8f\uff08sort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>sort()<\/code> \u51fd\u6570\u5bf9\u6570\u7ec4\u7684\u503c\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u5e76\u4e14\u91cd\u65b0\u6392\u5217\u6570\u7ec4\u7684\u7d22\u5f15\u3002\u8be5\u51fd\u6570\u4f1a\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6216\u6570\u5b57\u7684\u5347\u5e8f\u5bf9\u6570\u7ec4\u6392\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$numbers = array(3, 1, 4, 1, 5);\nsort($numbers);\nprint_r($numbers);  \/\/ \u8f93\u51fa: Array ( &#91;0] =&gt; 1 &#91;1] =&gt; 1 &#91;2] =&gt; 3 &#91;3] =&gt; 4 &#91;4] =&gt; 5 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>\u6309\u503c\u6392\u5e8f\uff08rsort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>rsort()<\/code> \u51fd\u6570\u4e0e <code>sort()<\/code> \u7c7b\u4f3c\uff0c\u4f46\u5b83\u662f\u6309\u964d\u5e8f\u5bf9\u6570\u7ec4\u503c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$numbers = array(3, 1, 4, 1, 5);\nrsort($numbers);\nprint_r($numbers);  \/\/ \u8f93\u51fa: Array ( &#91;0] =&gt; 5 &#91;1] =&gt; 4 &#91;2] =&gt; 3 &#91;3] =&gt; 1 &#91;4] =&gt; 1 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u4fdd\u6301\u952e\u503c\u5173\u7cfb\u6392\u5e8f\uff08asort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>asort()<\/code> \u51fd\u6570\u6309\u503c\u5347\u5e8f\u6392\u5e8f\u6570\u7ec4\uff0c\u4f46\u4fdd\u6301\u6570\u7ec4\u4e2d\u7684\u952e\u503c\u5173\u7cfb\u4e0d\u53d8\u3002\u8fd9\u5728\u5904\u7406\u5173\u8054\u6570\u7ec4\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$colors = array(\"red\" =&gt; 2, \"green\" =&gt; 3, \"blue\" =&gt; 1);\nasort($colors);\nprint_r($colors);  \/\/ \u8f93\u51fa: Array ( &#91;blue] =&gt; 1 &#91;red] =&gt; 2 &#91;green] =&gt; 3 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u6309\u952e\u6392\u5e8f\uff08ksort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>ksort()<\/code> \u51fd\u6570\u6309\u7167\u6570\u7ec4\u7684\u952e\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u800c\u4e0d\u6539\u53d8\u503c\u7684\u987a\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$colors = array(\"red\" =&gt; 2, \"green\" =&gt; 3, \"blue\" =&gt; 1);\nksort($colors);\nprint_r($colors);  \/\/ \u8f93\u51fa: Array ( &#91;blue] =&gt; 1 &#91;green] =&gt; 3 &#91;red] =&gt; 2 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u6309\u952e\u6392\u5e8f\uff08krsort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>krsort()<\/code> \u51fd\u6570\u4e0e <code>ksort()<\/code> \u76f8\u53cd\uff0c\u5b83\u662f\u6309\u952e\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$colors = array(\"red\" =&gt; 2, \"green\" =&gt; 3, \"blue\" =&gt; 1);\nkrsort($colors);\nprint_r($colors);  \/\/ \u8f93\u51fa: Array ( &#91;red] =&gt; 2 &#91;green] =&gt; 3 &#91;blue] =&gt; 1 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>\u81ea\u7136\u987a\u5e8f\u6392\u5e8f\uff08natsort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>natsort()<\/code> \u51fd\u6570\u6309\u201c\u81ea\u7136\u987a\u5e8f\u201d\u5bf9\u6570\u7ec4\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u610f\u5473\u7740\u5b83\u5c06\u6570\u503c\u90e8\u5206\u6309\u5176\u6570\u5b57\u5927\u5c0f\u6392\u5e8f\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$items = array(\"a10\", \"a2\", \"a1\", \"a21\");\nnatsort($items);\nprint_r($items);  \/\/ \u8f93\u51fa: Array ( &#91;0] =&gt; a1 &#91;2] =&gt; a2 &#91;1] =&gt; a10 &#91;3] =&gt; a21 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\uff08usort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>usort()<\/code> \u51fd\u6570\u5141\u8bb8\u4f60\u4f20\u9012\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\uff0c\u4ee5\u4fbf\u6839\u636e\u7279\u5b9a\u7684\u6392\u5e8f\u89c4\u5219\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$numbers = array(3, 1, 4, 1, 5);\nusort($numbers, function($a, $b) {\n    return $a - $b;  \/\/ \u5347\u5e8f\n});\nprint_r($numbers);  \/\/ \u8f93\u51fa: Array ( &#91;0] =&gt; 1 &#91;1] =&gt; 1 &#91;2] =&gt; 3 &#91;3] =&gt; 4 &#91;4] =&gt; 5 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">8. <strong>\u6570\u7ec4\u5012\u5e8f\u6392\u5e8f\uff08array_reverse()\uff09<\/strong><\/h2>\n\n\n\n<p><code>array_reverse()<\/code> \u51fd\u6570\u5c06\u6570\u7ec4\u5143\u7d20\u7684\u987a\u5e8f\u5012\u8f6c\uff0c\u4f46\u4e0d\u4f1a\u6539\u53d8\u952e\u503c\u5bf9\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$numbers = array(3, 1, 4, 1, 5);\n$reversed = array_reverse($numbers);\nprint_r($reversed);  \/\/ \u8f93\u51fa: Array ( &#91;0] =&gt; 5 &#91;1] =&gt; 1 &#91;2] =&gt; 4 &#91;3] =&gt; 1 &#91;4] =&gt; 3 )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">9. <strong>\u6392\u5e8f\u7ed3\u5408\u4fdd\u7559\u952e\u540d\uff08arsort() \u548c ksort()\uff09<\/strong><\/h2>\n\n\n\n<p><code>arsort()<\/code> \u51fd\u6570\u6309\u503c\u964d\u5e8f\u6392\u5217\u6570\u7ec4\uff0c\u4fdd\u7559\u952e\u503c\u5173\u7cfb\uff0c\u800c <code>ksort()<\/code> \u662f\u6309\u952e\u8fdb\u884c\u5347\u5e8f\u6392\u5217\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$colors = array(\"red\" =&gt; 2, \"green\" =&gt; 3, \"blue\" =&gt; 1);\narsort($colors);\nprint_r($colors);  \/\/ \u8f93\u51fa: Array ( &#91;green] =&gt; 3 &#91;red] =&gt; 2 &#91;blue] =&gt; 1 )<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/ref.array.php\">PHP \u5b98\u65b9\u6392\u5e8f\u51fd\u6570\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/function.sort.php\">PHP \u6570\u7ec4\u6392\u5e8f\u793a\u4f8b<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u662f\u5e38\u7528\u7684 PHP \u6570\u7ec4\u6392\u5e8f\u51fd\u6570\uff0c\u5b83\u4eec\u5728\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u7ec4\u65f6\u53ef\u4ee5\u63d0\u4f9b\u5e2e\u52a9\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\uff0c\u6570\u7ec4\u6392\u5e8f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0cPHP \u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u5305\u62ec\u6309\u503c\u6392\u5e8f\u3001\u6309\u952e\u6392\u5e8f\u4ee5\u53ca\u81ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2983,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-2982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2982"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2982\/revisions"}],"predecessor-version":[{"id":2984,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2982\/revisions\/2984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2983"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}