Home  >  Article  >  php教程  >  php 数组自定义排序:php二维数组自定义排序

php 数组自定义排序:php二维数组自定义排序

WBOY
WBOYOriginal
2016-06-21 08:50:06998browse

//对二维数组自定义排序
function array_sort($arr,$keys,$type){
$keysvalue=array();
$i = 0;
foreach($arr as $key=>$val) {
$val[$keys] = str_replace("-","",$val[$keys]);
$val[$keys] = str_replace(" ","",$val[$keys]);
$val[$keys] = str_replace(":","",$val[$keys]);
$keysvalue[] =$val[$keys];
}
asort($keysvalue); //key值排序
reset($keysvalue); //指针重新指向数组第一个
foreach($keysvalue as $key=>$vals) {
$keysort[] = $key;
}
$new_array = array();
if($type != "asc"){
for($ii=count($keysort)-1; $ii>=0; $ii--) {
$new_array[] = $arr[$keysort[$ii]];
}
}else{
for($ii=0; $ii$new_array[] = $arr[$keysort[$ii]];
}
}
return $new_array;
}
$arr = array(
array(
'name' => '学习',
'size' => '1235',
'type' => 'jpe',
'time' => '1921-11-13',
'class' => 'dd',
),
array(
'name' => '中国功夫',
'size' => '153',
'type' => 'jpe',
'time' => '2005-11-13',
'class' => 'jj',
),
array(
'name' => '编程',
'size' => '35',
'type' => 'gif',
'time' => '1997-11-13',
'class' => 'dd',
),
array(
'name' => '中国功夫',
'size' => '65',
'type' => 'jpe',
'time' => '1925-02-13',
'class' => 'yy',
),
);
echo '
';<br>print_r(array_sort($arr,'size','asc'));<br>function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")<br>{<br>if(!is_array($ArrayData))<br>{<br>return $ArrayData;<br>}<br>// Get args number.<br>$ArgCount = func_num_args();<br>// Get keys to sort by and put them to SortRule array.<br>for($I = 1;$I {<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>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>?>
   本文链接http://www.cxybl.com/html/wlbc/Php/20130326/37392.html
            <p style="width:100%;text-align:center;margin:10px 0">

<br>
<br>

</p><p style="width:100%;text-align:center;margin:10px 0">

            </p><p class="clear"></p>

           
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn