{"id":750,"date":"2009-11-20T21:34:00","date_gmt":"2009-11-20T01:34:00","guid":{"rendered":""},"modified":"2013-11-26T14:17:04","modified_gmt":"2013-11-26T06:17:04","slug":"php%e6%8e%92%e5%ba%8f%e5%87%bd%e6%95%b0array_multisort%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/750.html","title":{"rendered":"PHP\u6392\u5e8f\u51fd\u6570array_multisort\u7528\u6cd5"},"content":{"rendered":"<p>\u4eca\u5929\u7814\u7a76\u4e86\u4e00\u4e0bphp\u7684array_multisort\uff0c\u7406\u89e3\u4e86\u4e4b\u540e\u53d1\u73b0\u4ed6\u5f88\u5f3a\u5927\uff0c\u800c\u4e14\u4e5f\u4e0d\u590d\u6742\u3002\uff08\u624b\u518c\u4e0a\u5173\u4e8e\u8fd9\u4e2a\u51fd\u6570\u7684\u8bb2\u89e3\u770b\u5f97\u5f88\u8d39\u529b\uff09<\/p>\n<p>\u4e00\u3001\u5148\u770b\u6700\u7b80\u5355\u7684\u60c5\u51b5\u3002\u6709\u4e24\u4e2a\u6570\u7ec4\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$arr1 = array(1,9,5);\r\n$arr2 = array(6,2,4);\r\n\r\narray_multisort($arr1,$arr2);\r\n\r\nprint_r($arr1); \/\/ \u5f97\u5230\u7684\u987a\u5e8f\u662f1,5,9\r\nprint_r($arr2); \/\/ \u5f97\u5230\u7684\u987a\u5e8f\u662f6,4,2\r\n<\/pre>\n<p>\u6211\u4f30\u8ba1\u4e24\u4e2a\u6570\u7ec4\u7684\u503c\u81ea\u59cb\u81f3\u7ec8\u90fd\u662f\u5bf9\u5e94\u7740\u7684\uff1a1\u5bf9\u5e946\uff0c9\u5bf9\u5e942\uff0c5\u5bf9\u5e944\u3002<\/p>\n<p>\u6211\u4eec\u518d\u52a0\u591a\u4e00\u4e2a\u6570\u7ec4\u770b\u770b\u4f1a\u600e\u6837\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$arr1 = array(1,9,5);\r\n$arr2 = array(6,2,4);\r\n$arr3 = array(3,7,8);\r\n\r\narray_multisort($arr1,$arr2,$arr3);\r\n<\/pre>\n<p>\u67e5\u770b\u7ed3\u679c\uff0c1\u81ea\u59cb\u81f3\u7ec8\u90fd\u5bf9\u5e946\u5bf9\u5e943\uff0c\u5176\u5b83\u9879\u4e5f\u662f\u5982\u6b64\u3002\u8fd9\u79cd\u5bf9\u5e94\u5173\u7cfb\u5c31\u662f\u624b\u518c\u4e2d\u6240\u8c13\u7684\u201c\u6392\u5e8f\u65f6\u4fdd\u7559\u539f\u6709\u7684\u952e\u540d\u5173\u8054\u201d\u3002<\/p>\n<p>\u53e6\u5916\u4e5f\u53ef\u4ee5\u628a\u6bcf\u4e2a\u6570\u7ec4\u60f3\u50cf\u6210\u6570\u636e\u5e93\u8868\u7684\u4e00\u5217\u3002\u800c\u5bf9\u5e94\u7740\u76841,6,3\u4e3a\u4e00\u6570\u636e\u884c\uff0c9,2,7\u4e3a\u53e6\u4e00\u6570\u636e\u884c\u3002\u3002\u3002<\/p>\n<p>array_multisort\u4f1a\u5148\u6309\u7b2c\u4e00\u4e2a\u6570\u7ec4\uff08\u60f3\u50cf\u6210\u5217\uff09\u6392\u5e8f\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u6570\u7ec4\uff08\u5217\uff09\u7684\u503c\u76f8\u540c\uff0c\u5219\u6309\u7b2c\u4e8c\u4e2a\u6570\u7ec4\uff08\u5217\uff09\u6392\u5e8f\u3002<\/p>\n<p>\u5177\u4f53\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u7a0b\u5f0f\u6765\u6d4b\u8bd5\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$arr1 = array(1,9,5,9);\r\n$arr2 = array(6,2,4,1);\r\n$arr3 = array(3,7,8,0);\r\n\r\narray_multisort($arr1,$arr2,$arr3);\r\n<\/pre>\n<p>\u53ef\u4ee5\u60f3\u50cf\u8fd9\u91cc$arr3\u7684\u7ed3\u679c\u662f(3,8,0,7)\u3002<\/p>\n<p>\u4e8c\u3001\u63a5\u4e0b\u6765\u8bb2\u89e3array_multisort\u7684\u53c2\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u5f88\u7075\u6d3b\u3002\u6700\u7b80\u5355\u7684\u60c5\u51b5\u662f\u5982\u4e0a\u9762\u6240\u793a\u7684\u4ee51\u4e2a\u6216n\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u6bcf\u4e2a\u6570\u7ec4\u7684\u9879\u6570\u8981\u4e00\u6837\uff0c\u5426\u5219\u4f1awarning\u5bfc\u81f4\u6392\u5e8f\u5931\u6548\u3002<\/p>\n<p>\u50cf\u8fd9\u6837array_multisort($arr1,$arr2,$arr3); \u9ed8\u8ba4\u662f\u6240\u6709\u6570\u7ec4\u90fd\u662f\u5347\u5e8f\u6392\u5217\uff0c\u5982\u679c\u60f3\u5bf9$arr2\u964d\u5e8f\uff0c\u5e76\u5f53\u4f5c\u5b57\u7b26\u4e32\u53bb\u6bd4\u8f83\uff0c\u5c31\u8981\u5199\u6210\uff1a<\/p>\n<p>array_multisort($arr1, $arr2, SORT_DESC, SORT_STRING, $arr3);<\/p>\n<p>\u6bcf\u4e2aarray\u540e\u9762\u53ef\u4ee5\u8ddf\u4e00\u4e2a\u6392\u5e8f\u987a\u5e8f\u6807\u5fd7\u6216\u4e00\u4e2a\u6392\u5e8f\u7c7b\u578b\u6807\u5fd7\uff0c\u6216\u8005\u4e24\u79cd\u6807\u5fd7\u540c\u65f6\u51fa\u73b0\u3002\u4f46\u662f\u6bcf\u79cd\u6392\u5e8f\u6807\u5fd7\u5728\u6bcf\u4e2a\u6570\u7ec4\u540e\u9762\u53ea\u80fd\u51fa\u73b0\u4e00\u4e2a\u3002<\/p>\n<p>\u8be6\u7ec6\u5982\u4e0b\uff1a<\/p>\n<p>\u6392\u5e8f\u987a\u5e8f\u6807\u5fd7\uff1a<\/p>\n<p>SORT_ASC \u2013 \u6309\u7167\u4e0a\u5347\u987a\u5e8f\u6392\u5e8f\uff08\u9ed8\u8ba4\uff09<\/p>\n<p>SORT_DESC \u2013 \u6309\u7167\u4e0b\u964d\u987a\u5e8f\u6392\u5e8f<\/p>\n<p>\u6392\u5e8f\u7c7b\u578b\u6807\u5fd7\uff1a<\/p>\n<p>SORT_REGULAR \u2013 \u5c06\u9879\u76ee\u6309\u7167\u901a\u5e38\u65b9\u6cd5\u6bd4\u8f83\uff08\u9ed8\u8ba4\uff09<\/p>\n<p>SORT_NUMERIC \u2013 \u5c06\u9879\u76ee\u6309\u7167\u6570\u503c\u6bd4\u8f83<\/p>\n<p>SORT_STRING \u2013 \u5c06\u9879\u76ee\u6309\u7167\u5b57\u7b26\u4e32\u6bd4\u8f83<\/p>\n<p>\u4e09\u3001\u6700\u540e\u662farray_multisort\u6709\u4ec0\u4e48\u5b9e\u9645\u4f5c\u7528\u3002<\/p>\n<p>\u6211\u4eec\u901a\u5e38\u6709\u4e00\u4e9b\u591a\u7ef4\u6570\u7ec4\u9700\u8981\u6392\u5e8f\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$guys = Array\r\n(\r\n    &#x5B;0] =&gt; Array\r\n        (\r\n            &#x5B;name] =&gt; jake\r\n            &#x5B;score] =&gt; 80\r\n            &#x5B;grade] =&gt; A\r\n        )\r\n\r\n    &#x5B;1] =&gt; Array\r\n        (\r\n            &#x5B;name] =&gt; jin\r\n            &#x5B;score] =&gt; 70\r\n            &#x5B;grade] =&gt; A\r\n        )\r\n\r\n    &#x5B;2] =&gt; Array\r\n        (\r\n            &#x5B;name] =&gt; john\r\n            &#x5B;score] =&gt; 80\r\n            &#x5B;grade] =&gt; A\r\n        )\r\n\r\n    &#x5B;3] =&gt; Array\r\n        (\r\n            &#x5B;name] =&gt; ben\r\n            &#x5B;score] =&gt; 20\r\n            &#x5B;grade] =&gt; B\r\n        )\r\n\r\n)\r\n<\/pre>\n<p>\u4f8b\u5982\u6211\u4eec\u60f3\u6309\u6210\u7ee9\u5012\u5e8f\u6392\u5217\uff0c\u5982\u679c\u6210\u7ee9\u76f8\u540c\u5c31\u6309\u540d\u5b57\u7684\u5347\u5e8f\u6392\u5217\u3002<br \/>\n\u8fd9\u65f6\u6211\u4eec\u5c31\u9700\u8981\u6839\u636e$guys\u7684\u987a\u5e8f\u591a\u5f04\u4e24\u4e2a\u6570\u7ec4\u51fa\u6765\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$scores = array(80,70,80,20);\r\n$names = array('jake','jin','john','ben');\r\n\u7136\u540e\r\narray_multisort($scores, SORT_DESC, $names, $guys);\u5c31\u884c\u4e86\r\n<\/pre>\n<p>\u8fd8\u80fd\u4e0d\u80fd\u66f4\u7075\u6d3b\u4e00\u70b9\u5462\uff0c\u6bcf\u6b21\u60f3\u6392\u5e8f\u90fd\u8981\u53e6\u5916\u5f04\u4e9b\u6570\u7ec4\u51fa\u6765\u5417\uff1f<br \/>\n\u5176\u5b9e\u5728qeephp\u7684helper_array\u7c7b\u91cc\u9762\u5df2\u7ecf\u5c01\u88c5\u5f97\u5f88\u597d\uff0c\u4e0b\u9762\u662f\u5b83\u7684\u4e24\u4e2a\u65b9\u6cd5\uff0c\u9700\u8981\u7684\u4eba\u81ea\u5df1\u4fee\u6539\u4e00\u4e0b\u5c31\u53ef\u4ee5\u7528\u4e86\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n           \/**\r\n            * \u6839\u636e\u6307\u5b9a\u7684\u952e\u5bf9\u6570\u7ec4\u6392\u5e8f\r\n            *\r\n            * \u7528\u6cd5\uff1a\r\n            * @code php\r\n            * $rows = array(\r\n            *            array('id' =&gt; 1, 'value' =&gt; '1-1', 'parent' =&gt; 1),\r\n            *            array('id' =&gt; 2, 'value' =&gt; '2-1', 'parent' =&gt; 1),\r\n            *            array('id' =&gt; 3, 'value' =&gt; '3-1', 'parent' =&gt; 1),\r\n            *            array('id' =&gt; 4, 'value' =&gt; '4-1', 'parent' =&gt; 2),\r\n            *            array('id' =&gt; 5, 'value' =&gt; '5-1', 'parent' =&gt; 2),\r\n            *            array('id' =&gt; 6, 'value' =&gt; '6-1', 'parent' =&gt; 3),\r\n            * );\r\n            *\r\n            * $rows = Helper_Array::sortByCol($rows, 'id', SORT_DESC);\r\n            * dump($rows);\r\n            * \/\/ \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\r\n            * \/\/ array(\r\n            * \/\/          array('id' =&gt; 6, 'value' =&gt; '6-1', 'parent' =&gt; 3),\r\n            * \/\/          array('id' =&gt; 5, 'value' =&gt; '5-1', 'parent' =&gt; 2),\r\n            * \/\/          array('id' =&gt; 4, 'value' =&gt; '4-1', 'parent' =&gt; 2),\r\n            * \/\/          array('id' =&gt; 3, 'value' =&gt; '3-1', 'parent' =&gt; 1),\r\n            * \/\/          array('id' =&gt; 2, 'value' =&gt; '2-1', 'parent' =&gt; 1),\r\n            * \/\/          array('id' =&gt; 1, 'value' =&gt; '1-1', 'parent' =&gt; 1),\r\n            * \/\/ )\r\n            * @endcode\r\n            *\r\n            * @param array $array \u8981\u6392\u5e8f\u7684\u6570\u7ec4\r\n            * @param string $keyname \u6392\u5e8f\u7684\u952e\r\n            * @param int $dir \u6392\u5e8f\u65b9\u5411\r\n            *\r\n            * @return array \u6392\u5e8f\u540e\u7684\u6570\u7ec4\r\n            *\/\r\n           static function sortByCol($array, $keyname, $dir = SORT_ASC)\r\n           {\r\n               return self::sortByMultiCols($array, array($keyname =&gt; $dir));\r\n           }\r\n           \/**\r\n            * \u5c06\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u6309\u7167\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u7c7b\u4f3c SQL \u8bed\u53e5\u4e2d\u7684 ORDER BY\r\n            *\r\n            * \u7528\u6cd5\uff1a\r\n            * @code php\r\n            * $rows = Helper_Array::sortByMultiCols($rows, array(\r\n            *            'parent' =&gt; SORT_ASC,\r\n            *            'name' =&gt; SORT_DESC,\r\n            * ));\r\n            * @endcode\r\n            *\r\n            * @param array $rowset \u8981\u6392\u5e8f\u7684\u6570\u7ec4\r\n            * @param array $args \u6392\u5e8f\u7684\u952e\r\n            *\r\n            * @return array \u6392\u5e8f\u540e\u7684\u6570\u7ec4\r\n            *\/\r\n           static function sortByMultiCols($rowset, $args)\r\n           {\r\n               $sortArray = array();\r\n               $sortRule = '';\r\n               foreach ($args as $sortField =&gt; $sortDir)\r\n               {\r\n                   foreach ($rowset as $offset =&gt; $row)\r\n                   {\r\n                       $sortArray&#x5B;$sortField]&#x5B;$offset] = $row&#x5B;$sortField];\r\n                   }\r\n                   $sortRule .= '$sortArray&#x5B;\\'' . $sortField . '\\'], ' . $sortDir . ', ';\r\n               }\r\n               if (empty($sortArray) || empty($sortRule)) { return $rowset; }\r\n               eval('array_multisort(' . $sortRule . '$rowset);');\r\n               return $rowset;\r\n           }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u7814\u7a76\u4e86\u4e00\u4e0bphp\u7684array_multisort\uff0c\u7406\u89e3\u4e86\u4e4b\u540e\u53d1\u73b0\u4ed6\u5f88\u5f3a\u5927\uff0c\u800c\u4e14\u4e5f\u4e0d\u590d\u6742\u3002\uff08\u624b\u518c\u4e0a\u5173\u4e8e\u8fd9\u4e2a\u51fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-750","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=750"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions"}],"predecessor-version":[{"id":5250,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions\/5250"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}