Bryian Tan
Это может быть полезно - Нативный PHP сортирует массив объектов по свойствам – Tormix Blog – Web development[^]
<?php
$attachment_ids = array();
$attachment_ids[0]['posts'] = 29;
$attachment_ids[0]['term_id'] = 49;
$attachment_ids[1]['posts'] = 2;
$attachment_ids[1]['term_id'] = 54;
$attachment_ids[2]['posts'] = 11;
$attachment_ids[2]['term_id'] = 36;
$attachment_ids = json_encode($attachment_ids);
$attachment_ids = json_decode($attachment_ids, FALSE);
// print_r($attachment_ids);
function sortArrayWithObjects($array, $property)
{
usort($array, function ($a, $b) use ($property) {
return (($a->$property == $b->$property) ? 0 : (($a->$property < $b->$property) ? -1 : 1));
//[short version] return strcmp($a->$property, $b->$property);
});
return $array;
}
$result = sortArrayWithObjects($attachment_ids, 'posts');
echo '<pre>';
var_dump($result);
echo '</pre>';
?>
Выход:
array(3) {
[0]=>
object(stdClass)#2 (2) {
["posts"]=>
int(2)
["term_id"]=>
int(54)
}
[1]=>
object(stdClass)#3 (2) {
["posts"]=>
int(11)
["term_id"]=>
int(36)
}
[2]=>
object(stdClass)#1 (2) {
["posts"]=>
int(29)
["term_id"]=>
int(49)
}
}