Sorting multidimensional array based on the order of plain array

后端 未结 5 1070
时光说笑
时光说笑 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 11:59

    That's what array_multisort if for:

    foreach ($routes as $key => $row) {
        $code[$key]  = $row['code'];
        $name[$key] = $row['name'];
    }
    array_multisort($code, SORT_ASC, $name, SORT_ASC, $routes);
    
    print_r( $routes );
    

    That way you don't even need a second array!

    In fact in your case you need only to sort the codes so this will do the trick:

    foreach ($routes as $key => $row) {
        $code[$key]  = $row['code'];
    }
    
    array_multisort($code, SORT_ASC, $routes);
    

提交回复
热议问题