c语言sscanf函数的用法是什么
262
2022-08-22
对PHP数组进行自定义排序
usort — 使用用户自定义的比较函数对数组中的值进行排序 Note: 此函数将为array填充新的键名,将会删除原有键名
function asc($a,$b){ return $a>$b?1:-1;}$b=array('2','6','8','9','1','7');usort($b,'asc');print_r($b);//输出 Array ( [0] => 1 [1] => 2 [2] => 6 [3] => 7 [4] => 8 [5] => 9 ) 判断条件为 return $a>$b?1:-1; 数组值按照从小到大排序 function re($a,$b){ $aa = array(7,5,3,1,2,4,9,8,6,9); foreach($aa as $k => $v){ if($a==$v){ $ai = $k; } if($b==$v){ $bi = $k; } } return ($ai>$bi)?1:-1;}$x=array( array('1','2','3','4','5','6'), array('2','6','8','9','1','8'), array('3','5','4','7','6','8'), array('4','5','8','1','9','6'), array('5','8','1','2','4','6'), );foreach($x as &$v){ usort($v,'re');}print_r($x);Array ( [0] => Array ( [0] => 5 [1] => 3 [2] => 1 [3] => 2 [4] => 4 [5] => 6 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 8 [3] => 8 [4] => 6 [5] => 9 ) [2] => Array ( [0] => 7 [1] => 5 [2] => 3 [3] => 4 [4] => 8 [5] => 6 ) [3] => Array ( [0] => 5 [1] => 1 [2] => 4 [3] => 8 [4] => 6 [5] => 9 ) [4] => Array ( [0] => 5 [1] => 1 [2] => 2 [3] => 4 [4] => 8 [5] => 6 ) )
这时候数组按照给定的顺序从左往右排序
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~