问题描述
这是数组的简化版本.通过"id"或"created_by"对它进行排序很容易,但是我很难弄清楚如何在元素"内按"e5e53240-1d5a-4b50-ad7d-cfa00f33badd"的值对其进行排序目的.我花了数小时来寻找其他问题,试图解决这个问题,但是没有运气.
Here is a simplified version of the array. It is easy to sort this by 'id', or 'created_by', but I'm having a hard time figuring out how to sort it by the value of "e5e53240-1d5a-4b50-ad7d-cfa00f33badd" inside the 'elements' object. I've spent hours searching through other questions trying to figure this out, but with no luck.
所以这是通过print_r简化的数组版本:
So here's the simplified version of the array via print_r:
[0] => stdClass Object
(
[id] => 12
[created_by] => 776
[searchable] => 1
[elements] => {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421": {
},
"e5e53240-1d5a-4b50-ad7d-cfa00f33badd": {
"0": {
"value": "Aeronca"
}
},
"d7c903a8-fa15-4620-9d9b-2238cb48fd5c": {
"0": {
"value": "L-3B"
}
}
)
[1] => stdClass Object
(
[id] => 21
[created_by] => 776
[searchable] => 1
[elements] => {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421": {
},
"e5e53240-1d5a-4b50-ad7d-cfa00f33badd": {
"0": {
"value": "BEECHCRAFT"
}
},
"d7c903a8-fa15-4620-9d9b-2238cb48fd5c": {
"0": {
"value": "N-35"
}
}
)
[2] => stdClass Object
(
[id] => 13
[created_by] => 776
[searchable] => 1
[elements] => {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421": {
},
"e5e53240-1d5a-4b50-ad7d-cfa00f33badd": {
"0": {
"value": "AEROSPORT"
}
},
"d7c903a8-fa15-4620-9d9b-2238cb48fd5c": {
"0": {
"value": ""
}
}
)
因此,基本上,对数组进行排序时,第一个元素应该是现在的位置,然后是第三个元素,然后是第二个元素,所以它看起来像这样:
So basically the array, when sorted, should have the first element be where it is now, followed by the third element, followed by the second, so it would look like this:
[0] => stdClass Object
(
[id] => 12
[created_by] => 776
[searchable] => 1
[elements] => {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421": {
},
"e5e53240-1d5a-4b50-ad7d-cfa00f33badd": {
"0": {
"value": "Aeronca"
}
},
"d7c903a8-fa15-4620-9d9b-2238cb48fd5c": {
"0": {
"value": "L-3B"
}
}
)
[1] => stdClass Object
(
[id] => 13
[created_by] => 776
[searchable] => 1
[elements] => {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421": {
},
"e5e53240-1d5a-4b50-ad7d-cfa00f33badd": {
"0": {
"value": "AEROSPORT"
}
},
"d7c903a8-fa15-4620-9d9b-2238cb48fd5c": {
"0": {
"value": ""
}
}
)
[2] => stdClass Object
(
[id] => 21
[created_by] => 776
[searchable] => 1
[elements] => {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421": {
},
"e5e53240-1d5a-4b50-ad7d-cfa00f33badd": {
"0": {
"value": "BEECHCRAFT"
}
},
"d7c903a8-fa15-4620-9d9b-2238cb48fd5c": {
"0": {
"value": "N-35"
}
}
)
请记住,实际的数组要复杂得多,并且数组中包含近1,000个元素,因此希望我使用的任何方法都可以最大程度地减少服务器上的负载.
Keep in mind that the actual array is far more complex, and contains close to 1,000 elements in the array, so hopefully whatever method I use would minimize load on the server.
推荐答案
您要创建自己的比较器函数并使用使用排序进行排序.
You want to create your own comparator function and use usort for sorting.
例如:
function myComparator($a, $b) {
return $a->someValue - $b->someValue;
}
在php.net中解释的返回值:
The return value explained in php.net:
在代码中要对数组进行排序的某个位置:
And somewhere in your code where you want to sort the array:
usort($arrayOfObjects, 'myComparator');
这篇关于如何通过每个元素中对象内部的值对对象的PHP数组排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!