|
<p><?php<br />/**<br />* @package BugFree<br />* @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $<br />*<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 <wwccss@263.net><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 /> <br /> // Get args number.<br /> $ArgCount = func_num_args();<br /> <br /> // Get keys to sort by_and_put them to SortRule array.<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[] = '$'.$Arg;<br /> }<br /> else<br /> {<br /> $SortRule[] = $Arg;<br /> }<br /> }<br /> <br /> // Get the values according to the keys_and_put them to array.<br /> foreach($ArrayData AS $Key => $Info)<br /> {<br /> foreach($KeyNameList AS $KeyName)<br /> {<br /> ${$KeyName}[$Key] = $Info[$KeyName];<br /> }<br /> }<br /> <br /> // Create the eval string_and_eval it.<br /> $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';<br /> eval ($EvalString);<br /> return $ArrayData;<br />}<br /> <br /> <br />//################# 示例 #################<br />$arr = array(<br /> array(<br /> 'name' => '学习',<br /> 'size' => '1235',<br /> 'type' => 'jpe',<br /> 'time' => '1921-11-13',<br /> 'class' => 'dd',<br /> ),<br /> array(<br /> 'name' => '中国功夫',<br /> 'size' => '153',<br /> 'type' => 'jpe',<br /> 'time' => '2005-11-13',<br /> 'class' => 'jj',<br /> ),<br /> array(<br /> 'name' => '编程',<br /> 'size' => '35',<br /> 'type' => 'gif',<br /> 'time' => '1997-11-13',<br /> 'class' => 'dd',<br /> ),<br /> array(<br /> 'name' => '中国功夫',<br /> 'size' => '65',<br /> 'type' => 'jpe',<br /> 'time' => '1925-02-13',<br /> 'class' => 'yy',<br /> ),<br /> array(<br /> 'name' => '中国功夫',<br /> 'size' => '5',<br /> 'type' => 'icon',<br /> 'time' => '1967-12-13',<br /> 'class' => 'rr',<br /> ),<br />);<br /> <br />print_r($arr);<br /> <br />//注意:按照数字方式排序时 153 比 65 小<br />$temp = sysSortArray($arr,"name","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");<br /> <br />print_r($temp);<br /> <br />?><br /></p> |
|