0

我有一个多维数组,如下所示(PHP):

Array
(
    [144320] => Array
        (
            [3568728] => Array
                (
                    [30832] => 30832
                )

            [3568884] => Array
                (
                    [30827] => 30827
                    [30828] => 30828
                    [30830] => 30830
                    [30831] => 30831
                    [30832] => 30832
                    [30837] => 30837
                    [30838] => 30838
                    [30839] => 30839
                    [30826] => 30826
                    [30808] => 30808
                    [30806] => 30806
                    [30807] => 30807
                    [30698] => 30698
                    [30601] => 30601
                    [30697] => 30697
                )

        )

    [144330] => Array
        (
            [3568731] => Array
                (
                    [30827] => 30827
                    [30839] => 30839
                    [30838] => 30838
                    [30837] => 30837
                    [30832] => 30832
                    [30831] => 30831
                    [30828] => 30828
                    [30830] => 30830
                    [30826] => 30826
                    [30806] => 30806
                    [30808] => 30808
                    [30807] => 30807
                    [30698] => 30698
                    [30697] => 30697
                    [30601] => 30601
                )

        )

    [144218] => Array
        (
            [3568753] => Array
                (
                    [30808] => 30808
                )

        )

    [144216] => Array
        (
            [3568732] => Array
                (
                    [30808] => 30808
                )

        )

)

该数组由以下代码填充:

$sql = "SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately'  order by created desc";

$q   = db_query($sql);

$user_alerts = array();

  while ($row  = db_fetch_array($q)) 
{   
   $user_alerts [$row['uid']]    [$row['alert_id']]  [$row['nid']] = $row['nid'];
}

从上面的 user_alerts 数组中,我想重新排列 [$row['nid']] 数组,对于重新排列,我想捕获 [$row['nid']] 数组,然后将其捕获到另一个数组中,我想重新排列 $ row['nid'] 数组我想将此 $row['nid'] 数组更新为原始 user_alerts 数组。

我怎么能做到这一点?我没有在谷歌上搜索到这个,所以把它放在适当的地方。

4

1 回答 1

1

您可以做的最好的事情是从查询中对其进行排序,例如:

SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately' 
ORDER BY created desc, nid

但是,如果您没有访问权限,或者由于其他原因需要原始排序,则需要递归地遍历数组并使用 PHP ksort 方法将最后一个排序级别重新分配给原始数组:

http://www.php.net/manual/en/function.ksort.php

foreach($user_alerts as $uid=>$user_alert) {
    foreach($alert as $alert_id=>$nids) {
        $user_alerts[$uid][$alert_id] = ksort($nids); 
    }   
}

希望能帮助到你!

于 2013-04-06T14:07:35.190 回答