0

I have an array like below

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

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

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

)

And I have another array with index to sort array(1,0,2) ,So With these I want following

 Array
     (

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

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

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

     )

I googled and Found Few solutions on stackoverflow , But None seems to be successful for me

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

print_r($myarr);

Which outputs following

(
    [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
  • Is there something wrong with my answer? Commented Feb 13, 2018 at 5:58

1 Answer 1

1

You need to use array_multisort.

$order =[1,0,2];
array_multisort($myarr, $order);

Var_dump($myarr);

https://3v4l.org/cCT9d

Sign up to request clarification or add additional context in comments.

2 Comments

this is much better solution.+1
@AlivetoDie Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.