sort a multidimensional array using array_multisort

后端 未结 4 1320
感情败类
感情败类 2020-12-17 17:07

I have this array

Array
(
    [0] => Array
        (
            [brand] => blah blah
            [location] => blah blah
            [address] =>         


        
4条回答
  •  庸人自扰
    2020-12-17 17:42

    If you want to avoid the looping you can use the array_column function to achieve your target. For Example,

    You want to sort below array with distance sort

    $arr = array( 
      0 => array( 'lat' => 34, 'distance' => 332.08 ),
      1 => array( 'lat' => 34, 'distance' => 5 ),
      2 => array( 'lat' => 34, 'distance' => 34 )
    );
    

    Using below single line your array will be sort by distance

    array_multisort( array_column( $arr, 'distance' ), SORT_ASC, SORT_NUMERIC, $arr );
    

    Now, $arr contain with sorted array by distance

提交回复
热议问题