首頁  >  文章  >  php教程  >  php 数组自定义排序:php二维数组自定义排序

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

WBOY
WBOY原創
2016-06-21 08:50:06998瀏覽

//对二维数组自定义排序
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>

           
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn