There is an array

Array ( [0] => Array ( [data] => qwerty-1 ) [1] => Array ( [data] => qwerty-2 ) [2] => Array ( [data] => qwerty-3 ) [3] => Array ( [data] => qwerty-4 ) ) 

You need to sort it using the sorting array (set the order of the elements)

 $order_arr = [3, 1, 2, 0]; // порядок элементов Array ( [0] => Array ( [data] => qwerty-4 ) [1] => Array ( [data] => qwerty-2 ) [2] => Array ( [data] => qwerty-3 ) [3] => Array ( [data] => qwerty-0 ) ) 
  • in your code (result) instead of qwerty-0 should be qwerty-1 - Alex
  • Do you really need to sort it out, or just output it in this order? - teran

3 answers 3

 $arr = [ ['data' => 'qwerty-1'], ['data' => 'qwerty-2'], ['data' => 'qwerty-3'], ['data' => 'qwerty-4'] ]; $order_arr = [3, 1, 2, 0]; $arrSorted = []; foreach ($order_arr as $idx) { $arrSorted[] = $arr[$idx]; } // print_r($arrSorted); 

    Without creating a new array:

     $order_arr = [3, 1, 2, 0]; $array = [11,22,33,44]; $temp = []; foreach ($order_arr as $key => $value) { // Используйте это условие если ожидается, что элементов на своих местах // больше, чем элементов, которые надо поменять местами // if (($key != $value) && !isset($temp[$value])) { if (!isset($temp[$value])) { // PHP 7.1++ // [$array[$key], $array[$value]] = [$array[$value], $array[$key]]; // $temp[$key] = TRUE; // PHP 7.1-- list($array[$key],$array[$value]) = array($array[$value],$array[$key]); $temp[$key] = TRUE; } } var_dump($array); 

    A small review:

    1) The method I proposed: the fastest and requires an average amount of memory;

    2) The method proposed by Kirill Korushkin : a little longer and a little more memory will be executed (by creating a new array);

    3) The method proposed by Alex : will be the longest, but the least take the memory.

    PS If the volumes of the arrays are not large - take any method that you visually like, if the arrays are large, then you should think about optimization, choose what is closer to you.

    • one
      It will become even faster if $temp[$key] = 1; and in_array replace with empty/isset . - u_mulder
    • @u_mulder idealno - Manitikyl
    • @Alex I took this into account, it will still be long by invoking an anonymous function every time. - Manitikyl
    • @Manitikyl yes, and in terms of speed (review) everything is correct (the last time the test showed a speed better than 1 option, but this is more a coincidence) - Alex
     $sort = [3, 1, 2, 0]; $array = array( 0=>array('data'=>'qwerty-1'), 1=>array('data'=>'qwerty-2'), 2=>array('data'=>'qwerty-3'), 3=>array('data'=>'qwerty-4') ); $sort = array_flip($sort); 

    Option 1 (with preservation of keys):

     uksort($array, function($a, $b) use ($sort) { return $sort[$a] > $sort[$b]; }); 

    Option 1 test

    Option 2 (without saving the keys):

     uksort($array, function($a, $b) use ($sort) { return $sort[$a] > $sort[$b]; }); $array = array_values($array); 

    Option 2 test

    • one
      Isn't $sort = array_flip($sort); better $sort = array_flip($sort); to move beyond the limits of the function? - Manitikyl
    • @Manitikyl, in principle, yes, you can move it out so as not to call array_flip every time, thanks) - Alex