PHP 二维数组排序

因为,在mysql4上不支持 group by 字段 order by 字段 desc  ,在mysql5上,没问题!



所以,只能让我想别的办法,想呀想,郁闷了半天,


因为,买的虚拟主机是mysql4的,无语,所以想到  二维数组排序!

 

以下是我的代码:

 

$sql_jj_clicks_count = "select zj_jj_id,sum(everynum) as clicks_num from zj_jj_clicks where s1='$sheng_bj' and s2='$shi_bj' group by zj_jj_id limit 0,10";
$result_jj_clicks_count = $h->sql($sql_jj_clicks_count);
while ($rs_jj_clicks_count = mysql_fetch_array($result_jj_clicks_count)) {
    $sql_jj_clicks_count_xx = "select jjtruename,href,shouji,zihao from zj_jj where id=".$rs_jj_clicks_count['zj_jj_id'];
    $result_jj_clicks_count_xx = $h->sql($sql_jj_clicks_count_xx);
    while ($rs_jj_clicks_count_xx = mysql_fetch_array($result_jj_clicks_count_xx)) {
       
        $array_jj_clicks_count[] = array("jjtruename"=>$rs_jj_clicks_count_xx['jjtruename'],"href"=>$rs_jj_clicks_count_xx['href'],"shouji"=>$rs_jj_clicks_count_xx['shouji'],"zihao"=>$rs_jj_clicks_count_xx['zihao'],"clicks_num"=>$rs_jj_clicks_count['clicks_num']);
    }
   
}

foreach ($array_jj_clicks_count as $key => $row) {
    $jjtruename[$key] = $row['jjtruename'];
    $href[$key] = $row['href'];
    $shouji[$key] = $row['shouji'];
    $zihao[$key] = $row['zihao'];
    $clicks_num[$key] = $row['clicks_num'];
}

array_multisort($clicks_num, SORT_DESC,$array_jj_clicks_count);
$smarty->assign("rs_jj_clicks_count",$array_jj_clicks_count);

 

 

实现的原因,参考了以下文章:

 

 

array_multisort() 对二维数组进行排序
数组
$roughData,我们打算按照accurancy排序。
Array
(
[0] => Array
(
[username] => 10yl
[accuracy] => 0.00
)

[1] => Array
(
[username] => 11yl
[accuracy] => 1.00
)

[2] => Array
(
[username] => 12yl
[accuracy] => 0.00
)

[3] => Array
(
[username] => 13yl
[accuracy] => 1.00
)
)
方法:
(1)提取accuracy列数组

foreach ($roughData as $key => $row) {
            $usernames[$key] = $row['username'];
            $accuracy[$key] = $row['accuracy'];
}

(2)进行排序
array_multisort($accuracy, SORT_ASC,$roughData);

当print_r($roughData);后我们将得到一个按accuracy升序排序的二维数组

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值