我有以下数组,数组的每个元素都有一个优先级元素。根据最大优先级值,我们如何生成新数组?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 ))因此,此处的最高优先级值为 3。因此,结果数组将如下所示。你能帮帮我吗?提前感谢您。Array ( [0] => stdClass Object ( [id] => 3 [name] => Category 3 [updated] => 2020-01-14 17:42:23 [priority] => 3 ) )
1 回答

沧海一幻觉
TA贡献1824条经验 获得超5个赞
您可以使用 usort 按降序对数组进行排序,然后创建元素的数组: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 关注
- 89 浏览
添加回答
举报
0/150
提交
取消