【发布时间】:2018-12-17 10:55:14
【问题描述】:
所以我从数据库中得到了这个结果:
object(Illuminate\Support\Collection)#538 (1) {
["items":protected]=>
array(3) {
[0]=>
object(stdClass)#536 (3) {
["col_header"]=>
string(7) "other_1"
["col_order"]=>
int(12)
["data"]=>
string(13) "asdgasdgfasdg"
}
[1]=>
object(stdClass)#545 (3) {
["col_header"]=>
string(7) "other_2"
["col_order"]=>
int(10)
["data"]=>
string(10) "dfhgsdhgsd"
}
[2]=>
object(stdClass)#533 (3) {
["col_header"]=>
string(7) "other_3"
["col_order"]=>
int(11)
["data"]=>
string(1) "s"
}
}
现在,如何根据 col_order 的值对其结果进行排序?结果应该是这样的:
object(Illuminate\Support\Collection)#538 (1) {
["items":protected]=>
array(3) {
[0]=>
object(stdClass)#545 (3) {
["col_header"]=>
string(7) "other_2"
["col_order"]=>
int(10)
["data"]=>
string(10) "dfhgsdhgsd"
}
[1]=>
object(stdClass)#533 (3) {
["col_header"]=>
string(7) "other_3"
["col_order"]=>
int(11)
["data"]=>
string(1) "s"
}
[2]=>
object(stdClass)#536 (3) {
["col_header"]=>
string(7) "other_1"
["col_order"]=>
int(12)
["data"]=>
string(13) "asdgasdgfasdg"
}
}
我在这里尝试过使用asort(),但它似乎只支持关联数组。有什么办法可以解决这个问题吗?
【问题讨论】:
-
为什么您没有在查询中使用 orderBy?正如我认为你正在使用 laravel elquent 所以你可以在你的查询中使用 orderBy(''col_order)
-
@MD.JubairMizan 谢谢你。我没有想到!它解决了我的问题,将在我未来的场景中使用答案,
标签: php arrays sorting arraycollection