php 数组排序 按值,php – 按值排序数组

我正在为我的论坛创建一个“固定”功能,我正在寻找一种方法将我的固定主题放在数组的开头,以便它们“卡在”页面的顶部.

如果主题没有固定,则topic_pinned = NULL,如果它被固定,则topic_pinned = 0.

该数组按topic_updated排序.固定主题需要在保留在页面顶部时按topic_updated保持排序,然后在固定主题下是非固定主题,也按topic_updated排序.

主题数组($forum_topic_results):

Array

(

[0] => Array

(

[topic_id] => 4

[topic_subject] => Test Subject #4

[topic_date] => 2015-09-10 18:34:18

[topic_by] => 1

[topic_pinned] =>

[topic_updated] => 2015-09-10 20:37:22

)

[1] => Array

(

[topic_id] => 3

[topic_subject] => Test Subject #3

[topic_date] => 2015-08-22 09:24:40

[topic_by] => 1

[topic_pinned] => 0

[topic_updated] => 2015-09-04 22:02:31

)

[2] => Array

(

[topic_id] => 2

[topic_subject] => Test Subject #2

[topic_date] => 2015-08-15 10:56:00

[topic_by] => 1

[topic_pinned] =>

[topic_updated] => 2015-09-04 19:45:32

)

[3] => Array

(

[topic_id] => 1

[topic_subject] => Test Subject #1

[topic_date] => 2015-08-30 19:48:17

[topic_by] => 1

[topic_pinned] => 0

[topic_updated] => 2015-09-03 00:44:38

)

)

PHP:

/**

* getAllTopics

*

* Retreives the topics of the chosen category from the `forum_topics` table.

*

* @param $cat_id

* @access public

*/

public function getAllTopics($cat_id=NULL)

{

$database=$this->database;

$database->query('SELECT topic_id, topic_subject, topic_date, topic_by, topic_pinned, topic_locked FROM forum_topics WHERE topic_cat = :catid ORDER BY topic_updated DESC', array(':catid' => $cat_id));

$result = $database->statement->fetchAll(PDO::FETCH_ASSOC);

return $result;

}

# Get topics

$forum_topic_results = $this->getAllTopics($_GET['cat']);

foreach($forum_topic_results as $forum_topic_row)

{

# Get user's username.

$topic_by=SearchUser($forum_topic_row['topic_by']);

$data.='

'.

'

'.

'

'.$forum_topic_row['topic_subject'].'

'.

'by '.$topic_by['username'].' on '.date('D M d, Y g:i a', strtotime($forum_topic_row['topic_date'])).

'

'.

'

';

}

结果我想:

Array

(

[0] => Array

(

[topic_id] => 3

[topic_subject] => Test Subject #3

[topic_date] => 2015-08-22 09:24:40

[topic_by] => 1

[topic_pinned] => 0

[topic_updated] => 2015-09-04 22:02:31

)

[1] => Array

(

[topic_id] => 1

[topic_subject] => Test Subject #1

[topic_date] => 2015-08-30 19:48:17

[topic_by] => 1

[topic_pinned] => 0

[topic_updated] => 2015-09-03 00:44:38

)

[2] => Array

(

[topic_id] => 4

[topic_subject] => Test Subject #4

[topic_date] => 2015-09-10 18:34:18

[topic_by] => 1

[topic_pinned] =>

[topic_updated] => 2015-09-10 20:37:22

)

[3] => Array

(

[topic_id] => 2

[topic_subject] => Test Subject #2

[topic_date] => 2015-08-15 10:56:00

[topic_by] => 1

[topic_pinned] =>

[topic_updated] => 2015-09-04 19:45:32

)

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值