-1

我有一个包含 5 个元素的 php 对象数组,如下所示

  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) 

我需要使用另一个数组 ex- 对数组进行排序

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

此数组值与“uid”的对象数组键匹配

请帮我用 PHP 做到这一点。

4

1 回答 1

2

您可以通过循环和使用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);
于 2015-03-18T10:43:46.170 回答