【问题标题】:PHP Multidimensional Array Sort array_reverse not working?PHP多维数组排序array_reverse不起作用?
【发布时间】:2013-03-11 01:15:53
【问题描述】:

我有一个 Multidimensional 数组,我正在狂热地尝试按日期正确排序。

到目前为止,我得到了:

usort($respArr, function($a, $b){
        $t1 = strtotime($a['PublishDate']);
        $t2 = strtotime($b['PublishDate']);
        return $t1 - $t2;
    });

array_reverse($respArr);
echo '<pre>';
print_r($respArr);
echo '</pre>';
echo '<hr />';

这是上面的一些输出:

Array
(
[34] => Array
    (
        [Title] => o7thwd: Partnered with Internet Services Inc., to offer complete online presence! http://t.co/1SgAF66Tbf @ISI_Cloud
        [Link] => http://twitter.com/o7thwd/statuses/306088158574022656
        [PublishDate] => 2/25/2013 12:07:58 PM
        [Image] => 
        [Description] => o7thwd: Partnered with Internet Services Inc., to offer complete online presence! http://t.co/1SgAF66Tbf @ISI_Cloud
    )

[35] => Array
    (
        [Title] => o7thwd: Hangin with Mama Bear giving her the basics of social networking
        [Link] => http://twitter.com/o7thwd/statuses/307916797066240000
        [PublishDate] => 3/2/2013 1:14:19 PM
        [Image] => 
        [Description] => o7thwd: Hangin with Mama Bear giving her the basics of social networking
    )

[36] => Array
    (
        [Title] => o7thwd: o7th Web Design Articles http://t.co/plP8OkB7So
        [Link] => http://twitter.com/o7thwd/statuses/308687737400205313
        [PublishDate] => 3/4/2013 4:17:46 PM
        [Image] => 
        [Description] => o7thwd: o7th Web Design Articles http://t.co/plP8OkB7So
    )

[37] => Array
    (
        [Title] => o7thwd: finished up my GUI for a YUI minifier: http://t.co/7DKLoCXJHw
        [Link] => http://twitter.com/o7thwd/statuses/309309871931682816
        [PublishDate] => 3/6/2013 9:29:54 AM
        [Image] => 
        [Description] => o7thwd: finished up my GUI for a YUI minifier: http://t.co/7DKLoCXJHw
    )

[38] => Array
    (
        [Title] => o7thwd: o7th Web Design Articles http://t.co/QAIUO2Qjko
        [Link] => http://twitter.com/o7thwd/statuses/314356123027243008
        [PublishDate] => 3/20/2013 8:41:54 AM
        [Image] => 
        [Description] => o7thwd: o7th Web Design Articles http://t.co/QAIUO2Qjko
    )

[39] => Array
    (
        [Title] => SkorIn2: starting new desktop app to compliment the web app
        [Link] => http://twitter.com/SkorIn2/statuses/314718985725804545
        [PublishDate] => 3/21/2013 8:43:48 AM
        [Image] => 
        [Description] => SkorIn2: starting new desktop app to compliment the web app
    )

)

然而,数组并没有颠倒,正如您在上面所说的那样。 谁能帮我解决这个问题?
它向我展示了该数组确实是按“PublishDate”排序的,但是,我需要它的相反顺序。

【问题讨论】:

    标签: php multidimensional-array sorting


    【解决方案1】:

    实际的问题是你没有将array_reverse 的结果分配给任何东西

    // change this
    array_reverse($respArr);
    
    // to this
    $respArr = array_reverse($respArr);
    

    【讨论】:

    • 未经测试:function reveseme(&amp;$array){$array=array_reverse($array);return;} $array=array(1,2,3,4,5); reveseme($array); 这应该可以。
    【解决方案2】:

    试试这个:

    $arr  = your array;
    $sort = array();
    foreach($arr as $k=>$v) {
        $sort['PublishDate'][$k] = $v['PublishDate'];
    }
    
    array_multisort($sort['PublishDate'], SORT_DESC, $arr);
    
    echo "<pre>";
    print_r($arr);
    

    但是要正确格式化您的日期,否则这可能行不通。将其格式化为yyyy-mm-dd hh:mm:ss

    【讨论】:

    • 抱歉,我无法选择格式...这是从 twitter 提要中提取的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多