PHP - 用另一个数组对数组进行排序

GCW*_*GCW 2 php arrays sorting

是否有一种简单的方法可以使用为此任务制作的变量数组对数组进行排序?例如:

$fruits [
   'Apple' => '12',
   'Cherry' => '10',
   'Lemon' => '34', 
   'Peach' => '6'
]

$order [
   1 => 'Peach',
   2 => 'Other',
   3 => 'Lemon',
   4 => 'Other2',
   5 => 'Apple',
   6 => 'Cherry',
   7 => 'Other3'
]
Run Code Online (Sandbox Code Playgroud)

我想返回这种数组:

$ordered_fruits [
   'Peach' => '6',
   'Lemon' => '34',
   'Apple' => '12',
   'Cherry' => '10'
]
Run Code Online (Sandbox Code Playgroud)

spl*_*h58 8

用php函数制作它:

$new = array_filter(array_replace(array_fill_keys($order, null), $fruits));
Run Code Online (Sandbox Code Playgroud)


Kan*_*iya 5

$ordered_fruits = array();
foreach($order as $value) {

   if(array_key_exists($value,$fruits)) {
      $ordered_fruits[$value] = $fruits[$value];
   }
}
Run Code Online (Sandbox Code Playgroud)