Сортировка массива объектов PHP с использованием другого обычного массива

У меня есть массив объектов php с 5 элементами, как показано ниже

  array(5) {
[0]=> object(stdClass)#2 (15) {["subject"]=> string(2) "dd" ["from"]=> string(31) "Brooks Hunt " ["to"]=> string(21) "test@spokaneautos.com" ["date"]=> string(30) "Mon, 9 Mar 2015 21:28:59 -0700" ["message_id"]=> string(37) "<00f501d05aea$bb0b6e40$31224ac0$@com>" ["size"]=> int(2822) ["uid"]=> int(18) ["msgno"]=> int(1) ["recent"]=> int(0) ["flagged"]=> int(0) ["answered"]=> int(0) ["deleted"]=> int(1) ["seen"]=> int(1) ["draft"]=> int(0) ["udate"]=> int(1425961775) }
[1]=> object(stdClass)#3 (15) { ["subject"]=> string(2) "33" ["from"]=> string(31) "Brooks Hunt " ["to"]=> string(21) "test@spokaneautos.com" ["date"]=> string(30) "Mon, 9 Mar 2015 21:29:42 -0700" ["message_id"]=> string(37) "<00fa01d05aea$d4c39920$7e4acb60$@com>" ["size"]=> int(2822) ["uid"]=> int(19) ["msgno"]=> int(2) ["recent"]=> int(0) ["flagged"]=> int(1) ["answered"]=> int(0) ["deleted"]=> int(0) ["seen"]=> int(1) ["draft"]=> int(0) ["udate"]=> int(1425961819) }
[2]=> object(stdClass)#4 (15) { ["subject"]=> string(3) "dss" ["from"]=> string(31) "Brooks Hunt " ["to"]=> string(21) "test@spokaneautos.com" ["date"]=> string(30) "Mon, 9 Mar 2015 21:38:44 -0700" ["message_id"]=> string(37) "<011301d05aec$17fe2e20$47fa8a60$@com>" ["size"]=> int(2825) ["uid"]=> int(24) ["msgno"]=> int(5) ["recent"]=> int(0) ["flagged"]=> int(1) ["answered"]=> int(0) ["deleted"]=> int(0) ["seen"]=> int(1) ["draft"]=> int(0) ["udate"]=> int(1425962361) }
[3]=> object(stdClass)#5 (15) { ["subject"]=> string(2) "dd" ["from"]=> string(31) "Brooks Hunt " ["to"]=> string(21) "test@spokaneautos.com" ["date"]=> string(30) "Mon, 9 Mar 2015 21:44:51 -0700" ["message_id"]=> string(37) "<011d01d05aec$f2a362c0$d7ea2840$@com>" ["size"]=> int(2822) ["uid"]=> int(26) ["msgno"]=> int(7) ["recent"]=> int(0) ["flagged"]=> int(1) ["answered"]=> int(0) ["deleted"]=> int(0) ["seen"]=> int(1) ["draft"]=> int(0) ["udate"]=> int(1425962728) }
[4]=> object(stdClass)#6 (15) { ["subject"]=> string(2) "mm" ["from"]=> string(31) "Brooks Hunt " ["to"]=> string(21) "test@spokaneautos.com" ["date"]=> string(30) "Mon, 9 Mar 2015 21:52:41 -0700" ["message_id"]=> string(37) "<012201d05aee$0aa5aad0$1ff10070$@com>" ["size"]=> int(2822) ["uid"]=> int(27) ["msgno"]=> int(8) ["recent"]=> int(0) ["flagged"]=> int(1) ["answered"]=> int(1) ["deleted"]=> int(0) ["seen"]=> int(1)

мне нужно отсортировать массив выше, используя другой массив
экс-

Array ( [0] => 19 [1] => 24 [2] => 26 [3] => 27 [4] => 18)

Значения этого массива сопоставляются с ключом массива объектов «uid»

Пожалуйста, помогите мне сделать это с помощью PHP.

-1

Решение

Вы можете сделать это с помощью цикла и использования array_map(), Что-то вроде следующего (не проверено!):

// $objectArray - Your array of objects
// $orderArray  - Your array specifying the sort order
$lookupArray = [];

foreach ($objectArray as $object) {
$lookupArray[$object->uid] = $object;
}

$sortedArray = array_map(function ($uid) use ($lookupArray) { return $lookupArray[$uid]; }, $orderArray);
2

Другие решения

Других решений пока нет …