Php 我将如何根据对象数组的一个元素属性对其进行排序?
我在PHP中有一个数组,它遵循以下结构:Php 我将如何根据对象数组的一个元素属性对其进行排序?,php,sorting,object,Php,Sorting,Object,我在PHP中有一个数组,它遵循以下结构: $soap->progress = ` 0 => object(stdClass)#3 (5) { ["step"]=> int(6) .... } 1 => object(stdClass)#4 (5) { ["step"]=> int(8) .... } 2 => object(stdClass)#5 (5) { ["step"]=> int(1) .... } 3
$soap->progress = `
0 =>
object(stdClass)#3 (5) {
["step"]=>
int(6)
....
}
1 =>
object(stdClass)#4 (5) {
["step"]=>
int(8)
....
}
2 =>
object(stdClass)#5 (5) {
["step"]=>
int(1)
....
}
3 =>
object(stdClass)#6 (5) {
["step"]=>
int(4)
....
}
4 =>
object(stdClass)#7 (5) {
["step"]=>
int(3)
....
}
.... and so on
我将如何按$soap->progress[x]->step
?使用对其进行排序
函数sortByStep($a,$b)
{
如果($a->step==$b->step){
返回0;
}
返回($a->步骤<$b->步骤)?-1:1;
}
usort($array,“sortByStep”);
使用
函数sortByStep($a,$b)
{
如果($a->step==$b->step){
返回0;
}
返回($a->步骤<$b->步骤)?-1:1;
}
usort($array,“sortByStep”);
如果需要对这些对象进行排序,可以使用usort
函数定义自己的自定义排序回调:
usort($arrayOfObjects, function($a, $b) {
if($a->step == $b->step) {
return 0;
else if($a->step > $b->step) {
return -1;
} else {
return 1;
}
});
如果需要对这些对象进行排序,可以使用
usort
函数定义自己的自定义排序回调:
usort($arrayOfObjects, function($a, $b) {
if($a->step == $b->step) {
return 0;
else if($a->step > $b->step) {
return -1;
} else {
return 1;
}
});
您在这里看到的似乎是一个对象数组,而不是问题标题所建议的对象。与相同,但将
plannedCompletionDate
替换为step
。您在这里看到的似乎是一个对象数组,不是问题标题所建议的对象。与相同,但将plannedCompletionDate
替换为step
。我收到此错误:致命错误:无法将stdClass类型的对象用作数组
是通过访问step属性而不是数组索引来更新答案,只是被var_转储中的括号稍微弄糊涂了:)我得到了这个错误:致命错误:无法将stdClass类型的对象用作数组
是的答案会随着访问step属性而不是数组索引而更新,只是被var_转储中的括号稍微弄糊涂了:)