• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP排序之二维数组的按照字母排序实现代码_php技巧

php 搞代码 4年前 (2022-01-26) 15次浏览 已收录 0个评论
 <BR><?php <BR>/** <BR>* Sort an two-dimension array by some level two items use array_multisort() function. <BR>* <BR>* sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……) <BR>* @author Chunsheng Wang  <BR>* @param array $ArrayData the array to sort. <BR>* @param string $KeyName1 the first item to sort by. <BR>* @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC") <BR>* @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING") <BR>* @return array sorted array. <BR>*/ <BR>function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR") <BR>{ <BR>if(!is_array($ArrayData)) <BR>{ <BR>return $ArrayData; <BR>} <BR>$ArgCount = func_num_args(); <BR>for($I = 1;$I < $ArgCount;$I ++) <BR>{ <BR>$Arg = func_get_arg($I); <BR>if(!eregi("SORT",$Arg)) <BR>{ <BR>$KeyNameList[] = $Arg; <BR>$SortRule[] = '<i>·本2文来源gaodai$ma#com搞$代*码网2</i><strong>搞gaodaima代码</strong>$'.$Arg; <BR>} <BR>else <BR>{ <BR>$SortRule[] = $Arg; <BR>} <BR>} <BR>foreach($ArrayData AS $Key => $Info) <BR>{ <BR>foreach($KeyNameList AS $KeyName) <BR>{ <BR>${$KeyName}[$Key] = $Info[$KeyName]; <BR>} <BR>} <BR>$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);'; <BR>eval ($EvalString); <BR>return $ArrayData; <BR>} <BR>//################# 示例 ################# <BR>$arr = array( <BR>array( <BR>'name' => '学习', <BR>'size' => '1235', <BR>'type' => 'jpe', <BR>'time' => '1921-11-13', <BR>'class' => 'D', <BR>), <BR>array( <BR>'name' => '中国功夫', <BR>'size' => '153', <BR>'type' => 'jpe', <BR>'time' => '2005-11-13', <BR>'class' => 'J', <BR>), <BR>array( <BR>'name' => '编程', <BR>'size' => '35', <BR>'type' => 'gif', <BR>'time' => '1997-11-13', <BR>'class' => 'A', <BR>), <BR>array( <BR>'name' => '中国功夫', <BR>'size' => '65', <BR>'type' => 'jpe', <BR>'time' => '1925-02-13', <BR>'class' => 'D', <BR>), <BR>array( <BR>'name' => '中国功夫', <BR>'size' => '5', <BR>'type' => 'icon', <BR>'time' => '1967-12-13', <BR>'class' => 'C', <BR>), <BR>); <BR>print_r($arr); <BR>//注意:按照数字方式排序时 153 比 65 小 <BR>$temp = sysSortArray($arr,"class","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING"); <BR>echo "<pre class="prettyprint linenums">"; <BR>print_r($temp); <BR>?> <BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP排序之二维数组的按照字母排序实现代码_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址