PHP如何通过两个索引对包含对象(stdClass)的对象(stdClass)进行排序?
我有以下需要排序的数据结构。要排序的第一个字段是“kategorie”,然后是“order_by”。重要的是,对象的键保持不变,URL被用于代码的其他部分PHP如何通过两个索引对包含对象(stdClass)的对象(stdClass)进行排序?,php,class,sorting,data-structures,stdout,Php,Class,Sorting,Data Structures,Stdout,我有以下需要排序的数据结构。要排序的第一个字段是“kategorie”,然后是“order_by”。重要的是,对象的键保持不变,URL被用于代码的其他部分 object(stdClass)#2 (31) { ["https://idp1.xxx/idp/shibboleth"]=> object(stdClass)#12 (4) { ["name"]=> string(19) "
object(stdClass)#2 (31) {
["https://idp1.xxx/idp/shibboleth"]=>
object(stdClass)#12 (4) {
["name"]=>
string(19) "test"
["data"]=>
string(39) "test"
["kategorie"]=>
string(1) "2"
["order_by"]=>
string(3) "200"
}
["https://idp2.xxx/idp/shibboleth"]=>
object(stdClass)#1 (4) {
["name"]=>
string(52) "test"
["data"]=>
string(60) "test"
["kategorie"]=>
string(1) "3"
["order_by"]=>
string(3) "110"
}
["https://idp3.xxx/idp/shibboleth"]=>
object(stdClass)#1 (4) {
["name"]=>
string(52) "test"
["data"]=>
string(60) "test"
["kategorie"]=>
string(1) "2"
["order_by"]=>
string(3) "120"
} ...
它应该是什么样子的:
object(stdClass)#2 (31) {
["https://idp3.xxx/idp/shibboleth"]=>
object(stdClass)#1 (4) {
["name"]=>
string(52) "test"
["data"]=>
string(60) "test"
["kategorie"]=>
string(1) "2"
["order_by"]=>
string(3) "120"
}
["https://idp1.xxx/idp/shibboleth"]=>
object(stdClass)#12 (4) {
["name"]=>
string(19) "test"
["data"]=>
string(39) "test"
["kategorie"]=>
string(1) "2"
["order_by"]=>
string(3) "200"
}
["https://idp2.xxx/idp/shibboleth"]=>
object(stdClass)#1 (4) {
["name"]=>
string(52) "test"
["data"]=>
string(60) "test"
["kategorie"]=>
string(1) "3"
["order_by"]=>
string(3) "110"
} ...
最简单的方法是什么?我只是不能把我的头绕在它周围
编辑:usort不适用于stdClass对象 这个数组\u multisort也不起作用
array_multisort(array_column($entries, 'kategorie'), SORT_ASC,
array_column($entries, 'order_by'), SORT_ASC,
$entries);
Edit2:Typcasting到array,使用上面的multisort就是答案。它没有按预期工作,因为您是按字符串排序的,所以
“10”<“2”
。您可以将这些列转换为数字,或使用SORT\u NUMERIC
标志:
array\u multisort(
数组_列($entries,'kategorie'),
按数字排序,
数组_列($entries,'order_by'),
按数字排序,
$entries
);
Title是错误的,它不应该按索引排序,应该按对象数据文件“order_by”和“kategorie”排序。这是否回答了您的问题?usort不适用于stdClass对象,数组列答案对我的dataWell没有任何作用,那么这个呢?如果数组来自json\u decode()
调用,则将true
作为第二个参数传递以获得关联数组。