如数组
代码如下 | 复制代码 |
Array ( [0] => Array ( [id] => 1146 [orderid] => 3 ) [1] => Array [2] => Array [3] => Array [4] => Array ) |
这么一个数据,想按orderid来排序。找不到内置的php函数,网上搜索了下,发现如下方法:
代码如下 | 复制代码 |
$asc_func = create_function('$a,$b',' $k = "orderid"; if($a[$k] == $b[$k]) return 0; return $a[$k]>$b[$k]?1:-1; '); usort($arrs,$asc_func); |
$arrs是原数组,$k="orderid"是要排序的字段
后来对上面代码进行了升级
代码如下 | 复制代码 |
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR") { if(!is_array($ArrayData)) { return $ArrayData; } // Get args number. // Get keys to sort by and put them to SortRule array. // Get the values according to the keys and put them to array. // Create the eval string and eval it. |