php数组排序方法研究

by pxz


发布于: 2011-03-18 所属分类: php 后端 标签: 排序  309


<h3 id="h3-1-sort-rsort"><a name="1. 简单的有序一维数组排序 sort rsort" class="reference-link"></a><span class="header-link octicon octicon-link"></span>1. 简单的有序一维数组排序 sort rsort</h3><pre><code>//从小到大排序 $arr = array(5,8,1,7,2); sort($arr); print_r($arr); //从大到小排序 $data = array(5,8,1,7,2); rsort($arr); print_r($arr); /* 运行结果: Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 ) Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 ) */ </code></pre><h3 id="h3-2-"><a name="2. 带键值的一维数组排序" class="reference-link"></a><span class="header-link octicon octicon-link"></span>2. 带键值的一维数组排序</h3><pre><code>//根据数组key值进行排序 ksort, krsort $arr = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain"); ksort($arr); print_r($arr); $arr = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain"); krsort($arr); print_r($arr); /* 运行结果: Array ( [DE] => Germany [ES] => Spain [IN] => India [US] => United States ) Array ( [US] => United States [IN] => India [ES] => Spain [DE] => Germany ) */ </code></pre><pre><code>//根据数组value值进行排序 asort arsort $arr = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain"); asort($arr); print_r($arr); $arr = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain"); arsort($arr); print_r($arr); /* 运行结果: Array ( [DE] => Germany [IN] => India [ES] => Spain [US] => United States ) Array ( [US] => United States [ES] => Spain [IN] => India [DE] => Germany ) */ </code></pre><h3 id="h3-3-natsort"><a name="3. 自然语言排序 natsort" class="reference-link"></a><span class="header-link octicon octicon-link"></span>3. 自然语言排序 natsort</h3><pre><code>//自然语言排序 $arr = array("book-1", "book-10", "book-100", "book-5"); sort($arr); print_r($arr); natsort($arr); print_r($arr); /* 运行结果 Array ( [0] => book-1 [1] => book-10 [2] => book-100 [3] => book-5 ) Array ( [0] => book-1 [3] => book-5 [1] => book-10 [2] => book-100 ) */ </code></pre><h3 id="h3-4-"><a name="4. 用户自定义的规则排序" class="reference-link"></a><span class="header-link octicon octicon-link"></span>4. 用户自定义的规则排序</h3><pre><code>//用户自定义的规则排序 usort //可应用于一维数组和多维数组的排序, 灵活性高 $arr = array( array('id'=>1, 'order'=>2), array('id'=>2, 'order'=>1), array('id'=>3, 'order'=>5), array('id'=>4, 'order'=>4), array('id'=>5, 'order'=>3), ); function sortByorder($a, $b) { if ($a['order'] == $b['order']) { return 0; } return $a['order'] > $b['order'] ? 1 : -1; } usort($arr, 'sortByorder'); print_r($arr); /* 运行结果 Array ( [0] => Array ( [id] => 2 [order] => 1 ) [1] => Array ( [id] => 1 [order] => 2 ) [2] => Array ( [id] => 5 [order] => 3 ) [3] => Array ( [id] => 4 [order] => 4 ) [4] => Array ( [id] => 3 [order] => 5 ) ) */ </code></pre><h3 id="h3-5-array_multisort-"><a name="5. 多维数组的排序 array_multisort (非常有用的一个函数)" class="reference-link"></a><span class="header-link octicon octicon-link"></span>5. 多维数组的排序 array_multisort (非常有用的一个函数)</h3><p><strong>用法1. 可对数据库结果集进行多个字段多次排序, 类似于 SQL 的 ORDER BY 子句的功能</strong></p> <pre><code>//对数据结果集排序 $arr = array( array('id'=>1, 'num'=>2, 'age'=>15), array('id'=>2, 'num'=>5, 'age'=>20), array('id'=>3, 'num'=>5, 'age'=>16), array('id'=>4, 'num'=>5, 'age'=>18), array('id'=>5, 'num'=>9, 'age'=>13), ); $num = array(2,5,5,5,9); $age = array(15,20,16,18,13); array_multisort($num, SORT_DESC, $age, SORT_ASC, $arr); print_r($arr); /* 上面排序相当于order by num desc, age asc 运行结果: Array ( [0] => Array ( [id] => 1 [num] => 2 [age] => 15 ) [1] => Array ( [id] => 3 [num] => 5 [age] => 16 ) [2] => Array ( [id] => 4 [num] => 5 [age] => 18 ) [3] => Array ( [id] => 2 [num] => 5 [age] => 20 ) [4] => Array ( [id] => 5 [num] => 9 [age] => 13 ) ) */ </code></pre><p><strong>用法2. 可以用来一次对多个数组进行排序, 后面的数据排序会根据前一数组的排序结果进行排序, 即最后一个数组根据前几个数组进行排序</strong></p> <pre><code>$a1 = array(12, 11, 100, 105, 5); $a2 = array(1, 2, 3, 4, 5); array_multisort($a1, SORT_DESC, $a2); var_dump($a1); var_dump($a2); /* 运行结果: Array ( [0] => 105 [1] => 100 [2] => 12 [3] => 11 [4] => 5 ) Array ( [0] => 4 [1] => 3 [2] => 1 [3] => 2 [4] => 5 ) */ </code></pre>

发表评论

暂无评论

搜索
用户登陆