使用自定义键重新排序多维数组

时间:2018-02-13 04:03:47

标签: php arrays

我有一个如下所示的数组

Array
(
    [0] => Array
        (
            [text] => one 
            [mp3] => 1.mp3
        )

    [1] => Array
        (
            [text] => two 
            [mp3] => 2.mp3
        )

    [2] => Array
        (
            [text] => three
            [mp3] => 3.mp3
        )

)

我有另一个带索引的数组来排序数组(1,0,2),所以我想要跟随

 Array
     (

         [0] => Array(
             [text] => two[mp3] => 2. mp3
         )

         [1] => Array(
             [text] => one[mp3] => 1. mp3
         )

         [2] => Array(
             [text] => three[mp3] => 3. mp3
         )

     )

我用Google搜索并在stackoverflow上找到了很少的解决方案,但是对我来说似乎没有成功

$order=array(1,0,2);
$orderedarray = array_merge(array_flip($order),$myarr);
$myarr = $orderedarray;

print_r($myarr);

以下哪些输出

(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => Array
        (
            [text] => one 
            [mp3] => 1.mp3
        )

    [4] => Array
        (
            [text] => two 
            [mp3] => 2.mp3
        )

    [5] => Array
        (
            [text] => three
            [mp3] => 3.mp3
        )

)

1 个答案:

答案 0 :(得分:1)

您需要使用array_multisort

toBoolean

https://3v4l.org/cCT9d