Sorting multidimensional array based on the order of plain array

后端 未结 5 1072
时光说笑
时光说笑 2021-01-28 11:06

I have this array:

  $routes = array(
  array(
     \'code\' => \'PZSA\',
     \'name\' => \'PLaza san antonio\',
  ),
  array(
     \'code\' => \'AVAD\         


        
5条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 11:47

    Rather than doing what is essentially a linear search for each element, it might be quickest to re-index your original array by code:

    // create the index
    $indexed = array();
    foreach($routes as $route) {
        $indexed[$route['code']] = $route;
    }
    
    // add each target to a new sorted array in order
    $sorted = array();
    foreach($target as $code) {
        $sorted[] = $indexed[$code];
    }
    

提交回复
热议问题