PHP 二维数组排序

来源:互联网 发布:java乘法表代码对齐 编辑:程序博客网 时间:2024/04/30 04:14

因为,在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升序排序的二维数组

 

 

 

原创粉丝点击