如何按 PHP 中的值对对象数组集合进行排序
How to sort Object Array Collection by value in PHP
所以我从数据库中得到了这个结果:
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()
,但它似乎只支持关联数组。有什么办法可以解决这个问题吗?
答案是 http://php.net/manual/en/function.usort.php - 它按用户定义的比较函数排序。在以下代码段中将 a
替换为 col_order
:
<?php
$class1 = new stdClass();
$class1->a = 1;
$class2 = new stdClass();
$class2->a = 2;
$class3 = new stdClass();
$class3->a = 3;
$input = [$class3,$class2,$class1];
var_dump($input);
usort($input, function($a, $b) {
if ($a->a == $b->a) {
return 0;
}
return ($a->a < $b->a) ? -1 : 1;
});
var_dump($input);
当您使用 Illuminate 时,您可能还想查看 https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html#method_orderBy 并使用它。
根据评论编辑:使用PHP 7+可以利用飞船运算符来简化比较功能:
usort($input, function($a, $b) {
return $a->a <=> $b->a;
});
您可以使用 usort()
$test = array(array("col_header" => "other_1",
"col_order" => 12,
"data" => "asdgasdgfasdg"),
array("col_header" => "other_2",
"col_order" => 10,
"data" => "dfhgsdhgsd"),
array("col_header" => "other_3",
"col_order" => 11,
"data" => "s"));
usort($test, function($a, $b)
{
if ($a["col_order"] == $b["col_order"])
return (0);
return (($a["col_order"] < $b["col_order"]) ? -1 : 1);
});
var_dump($test);
输出:
array (size=3)
0 =>
array (size=3)
'col_header' => string 'other_2' (length=7)
'col_order' => int 10
'data' => string 'dfhgsdhgsd' (length=10)
1 =>
array (size=3)
'col_header' => string 'other_3' (length=7)
'col_order' => int 11
'data' => string 's' (length=1)
2 =>
array (size=3)
'col_header' => string 'other_1' (length=7)
'col_order' => int 12
'data' => string 'asdgasdgfasdg' (length=13)
但我建议您直接从 SQL 查询中对结果进行排序:
SELECT *
FROM yourTable
...
ORDER BY col_order ASC
所以我从数据库中得到了这个结果:
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()
,但它似乎只支持关联数组。有什么办法可以解决这个问题吗?
答案是 http://php.net/manual/en/function.usort.php - 它按用户定义的比较函数排序。在以下代码段中将 a
替换为 col_order
:
<?php
$class1 = new stdClass();
$class1->a = 1;
$class2 = new stdClass();
$class2->a = 2;
$class3 = new stdClass();
$class3->a = 3;
$input = [$class3,$class2,$class1];
var_dump($input);
usort($input, function($a, $b) {
if ($a->a == $b->a) {
return 0;
}
return ($a->a < $b->a) ? -1 : 1;
});
var_dump($input);
当您使用 Illuminate 时,您可能还想查看 https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html#method_orderBy 并使用它。
根据评论编辑:使用PHP 7+可以利用飞船运算符来简化比较功能:
usort($input, function($a, $b) {
return $a->a <=> $b->a;
});
您可以使用 usort()
$test = array(array("col_header" => "other_1",
"col_order" => 12,
"data" => "asdgasdgfasdg"),
array("col_header" => "other_2",
"col_order" => 10,
"data" => "dfhgsdhgsd"),
array("col_header" => "other_3",
"col_order" => 11,
"data" => "s"));
usort($test, function($a, $b)
{
if ($a["col_order"] == $b["col_order"])
return (0);
return (($a["col_order"] < $b["col_order"]) ? -1 : 1);
});
var_dump($test);
输出:
array (size=3)
0 =>
array (size=3)
'col_header' => string 'other_2' (length=7)
'col_order' => int 10
'data' => string 'dfhgsdhgsd' (length=10)
1 =>
array (size=3)
'col_header' => string 'other_3' (length=7)
'col_order' => int 11
'data' => string 's' (length=1)
2 =>
array (size=3)
'col_header' => string 'other_1' (length=7)
'col_order' => int 12
'data' => string 'asdgasdgfasdg' (length=13)
但我建议您直接从 SQL 查询中对结果进行排序:
SELECT *
FROM yourTable
...
ORDER BY col_order ASC