【无敌难的算法题】如何对以下特殊格式二维数组进行排序 本帖最后由 zcj2247 于 2012-04-11 16:31:02 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 别用var_dump 看着乱,把数组var_export出来 手册里array_multisort或者usort函数的例子你都看过了吗? 让你骗一次function func($a, $b){ return current($a) < current($b);}uasort($arr, "func");这样差不多了 帮你解答问题,还要帮你做数据,下次不可以这样了数据要用 var_export 函数输出的结果。这样才方便解决问题$ar = array( 53 => array( "http://c.com" => 3, "c.com" => 19452, ), 2=> array( "http://fff.com/index.html" => 9, "fff.com" => 944, ), 101 => array( "http://now.bbb.com/" => 1031, "now.bbb.com" => 0, ), 102 => array( "http://ccc.aaa.com/" => 978, "ccc.aaa.com" => 9242, ),);foreach($ar as $v) $t[] = current($v);array_multisort($t, SORT_DESC, $ar);print_r($ar);Array( [0] => Array ( [http://now.bbb.com/] => 1031 [now.bbb.com] => 0 ) [1] => Array ( [http://ccc.aaa.com/] => 978 [ccc.aaa.com] => 9242 ) [2] => Array ( [http://fff.com/index.html] => 9 [fff.com] => 944 ) [3] => Array ( [http://c.com] => 3 [c.com] => 19452 )) 这个方法索引KEY被重置了, 要保持索引KEY呀哥哥。。 $arr = array( 53=>array('http://c.com'=>3,'c.com'=>3),2=>array('http://c.comdd'=>9,'ccc.com'=>9),101=>array('http://dddd.com'=>1031,'dddd.com'=>112344),102=>array('http://vvv.com'=>900,'vvv.com'=>19232));$new_arr = array();foreach($arr as $k=>$v){ $new_arr[$k] = current($v);}arsort($new_arr);foreach($new_arr as $k=>$v){ $a[$k] = $arr[$k];}print_r($a);2个foreach 1个排序函数。能实现你要的效果,不过就是太繁琐多了。其他好的方法正在寻找。 本帖最后由 xuzuning 于 2012-04-12 10:05:03 编辑 晕,咋这么多的排序问题呢,刚回完一个又看到一个uasort($arr,create_function('$v1,$v2','return current($v1) < current($v2);')); 正则的问题! php-无头冤尸... 为什么mysql以前关键字不冲突,现在却冲突了? php初学者需要帮助啊 使用ob_start生成静态页面在.net中有没有类似的方法 php小问题 mail函数群发遇到问题 php历史上最简单的问题,可惜我不会,各位大哥帮个忙罗 怎样得到两个datetime类型的变量之间相差的天数? Mysql 的系统问题??? IMAP中的imap_fetchbody,imap_fetchstructure具体怎么用 PHP开发是否使用框架
{
return current($a) < current($b);
}
uasort($arr, "func");这样差不多了
数据要用 var_export 函数输出的结果。这样才方便解决问题
$ar = array(
53 => array(
"http://c.com" => 3,
"c.com" => 19452,
),
2=> array(
"http://fff.com/index.html" => 9,
"fff.com" => 944,
),
101 => array(
"http://now.bbb.com/" => 1031,
"now.bbb.com" => 0,
),
102 => array(
"http://ccc.aaa.com/" => 978,
"ccc.aaa.com" => 9242,
),
);foreach($ar as $v) $t[] = current($v);
array_multisort($t, SORT_DESC, $ar);
print_r($ar);Array
(
[0] => Array
(
[http://now.bbb.com/] => 1031
[now.bbb.com] => 0
) [1] => Array
(
[http://ccc.aaa.com/] => 978
[ccc.aaa.com] => 9242
) [2] => Array
(
[http://fff.com/index.html] => 9
[fff.com] => 944
) [3] => Array
(
[http://c.com] => 3
[c.com] => 19452
))
53=>array('http://c.com'=>3,'c.com'=>3),2=>array('http://c.comdd'=>9,'ccc.com'=>9),101=>array('http://dddd.com'=>1031,'dddd.com'=>112344),102=>array('http://vvv.com'=>900,'vvv.com'=>19232)
);$new_arr = array();
foreach($arr as $k=>$v){
$new_arr[$k] = current($v);
}
arsort($new_arr);foreach($new_arr as $k=>$v){
$a[$k] = $arr[$k];
}
print_r($a);
2个foreach 1个排序函数。能实现你要的效果,不过就是太繁琐多了。
其他好的方法正在寻找。
uasort($arr,create_function('$v1,$v2','return current($v1) < current($v2);'));