目錄
回覆內容:
首頁 後端開發 php教程 array中依照key排序的問題?

array中依照key排序的問題?

Dec 01, 2016 am 12:56 AM
arraylist php

<code>$test=array(60) {
 [0]=&gt; object(stdClass)#14 (3) { ["id"]=&gt; string(3) "609" ["title"]=&gt; string(19) "Physical Mechanisms" ["publish_time"]=&gt; string(10) "6 MAY 2016" }
 [1]=&gt; object(stdClass)#14 (3) { ["id"]=&gt; string(3) "610" ["title"]=&gt; string(20) "Engineering Substrate" ["publish_time"]=&gt; string(10) "2 MAY 2016" }
  ...}
 

</code>
登入後複製
登入後複製

依照publish_time對$test進行排序,怎麼做?請教!

回覆內容:

<code>$test=array(60) {
 [0]=&gt; object(stdClass)#14 (3) { ["id"]=&gt; string(3) "609" ["title"]=&gt; string(19) "Physical Mechanisms" ["publish_time"]=&gt; string(10) "6 MAY 2016" }
 [1]=&gt; object(stdClass)#14 (3) { ["id"]=&gt; string(3) "610" ["title"]=&gt; string(20) "Engineering Substrate" ["publish_time"]=&gt; string(10) "2 MAY 2016" }
  ...}
 

</code>
登入後複製
登入後複製

依照publish_time對$test進行排序,怎麼做?請教!

可以用php函數 usort 自訂排序

<code>&lt;?php
function _usort($a, $b) {
    if ( strtotime($a-&gt;publish_time) &lt; strtotime($a-&gt;publish_time) ) {
        return -1;
    } else if( strtotime($a-&gt;publish_time) &gt; strtotime($a-&gt;publish_time) ){
        return 1;
    } else {
        return 0;
    }
}

usort($arr, '_usort');</code>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

See all articles