Sorting multidimensional array based on the order of plain array

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

I have this array:

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


        
5条回答
  •  忘掉有多难
    2021-01-28 12:07

    $target_flip = array_flip($target);
    
    usort($routes, function($a, $b) use ($target_flip){
        return ($target_flip[$a['code']] < $target_flip[$b['code']]) ? -1 : 1;
    });
    

    Demo:

    • http://codepad.viper-7.com/DGDbAr

    Docs:

    • http://php.net/manual/en/function.array-flip.php
    • http://php.net/manual/en/function.usort.php
    • http://php.net/manual/en/functions.anonymous.php

提交回复
热议问题