我有以下數(shù)組,數(shù)組的每個元素都有一個優(yōu)先級元素。根據(jù)最大優(yōu)先級值,我們?nèi)绾紊尚聰?shù)組?Array( [0] => stdClass Object ( [id] => 1 [name] => Main Category 1 [updated] => 2020-01-14 14:13:30 [priority] => 1 ) [1] => stdClass Object ( [id] => 2 [name] => Main Category 2 [updated] => 2020-01-14 17:19:35 [priority] => 2 ) [2] => stdClass Object ( [id] => 3 [name] => Category 3 [updated] => 2020-01-14 17:42:23 [priority] => 3 ))因此,此處的最高優(yōu)先級值為 3。因此,結果數(shù)組將如下所示。你能幫幫我嗎?提前感謝您。Array ( [0] => stdClass Object ( [id] => 3 [name] => Category 3 [updated] => 2020-01-14 17:42:23 [priority] => 3 ) )
1 回答

滄海一幻覺
TA貢獻1824條經(jīng)驗 獲得超5個贊
您可以使用 usort 按降序?qū)?shù)組進行排序,然后創(chuàng)建元素的數(shù)組:priority[0]
usort($array, function ($a, $b) { return $b->priority - $a->priority; });
print_r(array($array[0]));
輸出:
Array
(
[0] => stdClass Object
(
[id] => 3
[name] => Category3
[updated] => 2020-01-1417:42:23
[priority] => 3
)
)
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報
0/150
提交
取消