<?php
$ver = array();
$ver[1] = array(1,5,7,9,10) ;
$ver[2] = array(6,7,7,10,100) ;
$ver[3] = array(1,5,8,5,3) ;
$ver[4] = array(10,5,70,11,10) ;
foreach(range(0,4) as $index){
echo '<hr>index:'.$index.'<'.'br>';
$a = sort_by_index($ver,$index); //call function
foreach($a as $item){
foreach ($item as $n)
printf('%03d ',$n);
echo '<'.'br>';
}
}
function sort_by_index($ver,$sort_index){
$a = $b = array(); $i=1;
foreach($ver as $k=>$v){
$a[$k] = $v[$sort_index];
}
asort($a);
foreach($a as $k=>$v){
$b[$i++] = $ver[$k];
}
return $b;
}
?>
แบบที่เร็วกว่า
<?php
$ver = array();
$ver[1] = array(1,5,7,9,10) ;
$ver[2] = array(6,7,7,10,100) ;
$ver[3] = array(1,5,8,5,3) ;
$ver[4] = array(10,5,70,11,10) ;
foreach(range(0,4) as $index){
echo '<hr>index:'.$index.'<'.'br>';
$a = array(); foreach ($ver as $key => $row) $a[$key] = $row[$index];
array_multisort($a, SORT_ASC, $ver);
foreach($ver as $item){
foreach ($item as $n)
printf('%03d ',$n);
echo '<'.'br>';
}
}
?>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น