PHP sort multidimensional array by other array

前端 未结 1 1709
栀梦
栀梦 2020-12-22 04:30

I have an array with IDs that looks like

array(
      0  => 12
      1  => 30
      2  => 50
      3  => 11
      4  => 22
      5  => 45
          


        
相关标签:
1条回答
  • 2020-12-22 04:34

    Assuming the array with the IDs is named $order and the array with the values is named $items:

    $keys = array_flip($order);
    
    usort($items, function($a, $b) use($keys)
    {
        return $keys[$a['id']] - $keys[$b['id']];
    });
    
    0 讨论(0)
提交回复
热议问题