這是數組的簡化版本。用'id'或'created_by'很容易排序,但我很難弄清楚如何通過'elements'中的「e5e53240-1d5a-4b50-ad7d-cfa00f33badd」目的。我花了幾個小時搜尋其他問題,試圖弄清楚這一點,但沒有運氣。如何使用每個元素中的對象內的值對PHP對象數組進行排序?
所以這裏的陣列通過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": ""
}
}
)
所以基本上陣列,排序的時候,應該有第一個元素是它在哪裏,現在,其次是第三個要素,其次是第二,所以它看起來像這樣:
[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"
}
}
)
請記住,實際的數組要複雜得多,並且包含接近陣列中的1000元,所以希望我用什麼方法將在服務器上減少負載。
我做了以下根據您的回答,它就像一個魅力: 功能myComparator($ A,$ B){ $ json_var = json_decode($ a-> elements,true); $ first = $ json_var ['e5e53240-1d5a-4b50-ad7d-cfa00f33badd'] [0] ['value']; $ json_var = json_decode($ b-> elements,true); $ second = $ json_var ['e5e53240-1d5a-4b50-ad7d-cfa00f33badd'] [0] ['value']; return strcmp($ first,$ second); } usort($ arrayOfObjects,'myComparator'); – user1905478