The example in this article describes how to implement a two-dimensional array sorting function using a PHP custom function. Share it with everyone for your reference, the details are as follows:
/**作用: 二维数组排序函数,支持多键名排序 * 返回: 排序好的数组 * 使用: array_msort(数组,需要排序的键名,排序方式); * 例子: array_msort($cflist,"chapter_orderid","SORT_ASC"); * array_msort($arr,"name","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING"); */ function array_msort($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR") { if(!is_array($ArrayData)) { return $ArrayData; } // 获取参数数量. $ArgCount = func_num_args(); // 排序,并放置到SortRule数组 for($i = 1;$i < $ArgCount;$i ++) { $Arg = func_get_arg($i); if(!eregi("SORT",$Arg)) { $KeyNameList[] = $Arg; $SortRule[] = '$'.$Arg; } else { $SortRule[] = $Arg; } } // Get the values according to the keys and put them to array. foreach($ArrayData AS $Key => $Info) { foreach($KeyNameList AS $KeyName) { ${$KeyName}[$Key] = $Info[$KeyName]; } } // Create the eval string and eval it. $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);'; eval($EvalString); return $ArrayData; }
Readers who are interested in more PHP-related content can check out the special topics of this site: "Complete PHP Array (Array) Operation Skills", "php Sorting Algorithm Summary", " Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Techniques Summary", "php regular expression usage summary", "PHP operations and operator usage" Summary", "Summary of PHP String Usage" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.
The above introduces the PHP multi-dimensional array PHP custom function to implement the two-dimensional array sorting function, including the content of PHP multi-dimensional array. I hope it will be helpful to friends who are interested in PHP tutorials.