2

Example:

$arr = array('willion','kevien','john','smith');

$orderedArr = array('anne','bold','hellen','john','kevien','polm','smith','willion');

And I want to resort the $arr order based on $orderdArr order. How do I do that?

The result I expect:

'john','kevien','smith','willion'

the $orderedArr may be changed in order,So I want the $arr's order to be the same as $orderedArr.

I am sorry for simplifed my question.I think my $arr is much more complexity,the $arr's strucuture is: I use print_r() to show the array.

array( [0] => array([id]=>1 [name]=>willion)
        [1] => array([id]=>2 [name]=>kevien)
        [2] => array([id]=>3 [name]=>join)
      )

Thank you very much!

1
  • What would be the expected result .... Commented Jun 6, 2013 at 8:55

5 Answers 5

3

You can use usort

$order = array_flip($orderedArr);
usort($arr, function ($a, $b) use($order) {
    return $order[$a] - $order[$b];
});
print_r($arr);

Output

Array
(
    [0] => john
    [1] => kevien
    [2] => smith
    [3] => willion
)

See Live DEMO

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

Comments

1

use foreach() on the ordered array, append the values to a new array, but only if the exist in the original:

$arr = array('willion','kevien','john','smith');
$orderedArr = array('anne','bold','hellen','john','kevien','polm','smith','willion');

foreach($orderedArr as $key => $value){
    if(in_array($value, $arr)){
        $newArr[] = $value;
    }
}

Outputs:

Array ( [0] => john [1] => kevien [2] => smith [3] => willion )

Comments

1

Assuming $arr is always a subset of $orderedArr, you can use array_intersect:

$arr = array_intersect($orderedArr, $arr);
// $arr is now ['john', 'kevien', 'smith', 'willion']

4 Comments

Yes, $arr always a subset of $orderedArr.
Just a simple intersect then :)
but the $arr is a multi-dimention array,so..not the same data structure of the $orderedArr
@qinHaiXiang it wasn't in the original question :)
1

If you don't want the id->name association of the $arr array you can simply do this.

$to_be_sorted=array();
foreach($arr as $key=>$person)
{
    $to_be_sorted[]=$person['name'];
}

$sorted = array_intersect($orderedArr, $to_be_sorted);

echo implode(",",$sorted);

Comments

0

I think you can loop in your order array and comparing to your array using in_array to build a new array in required order

$arr = array('willion','kevien','john','smith');

$orderedArr = array('anne','bold','hellen','john','kevien','polm','smith','willion');

$orderedUsers = array();
foreach ($orderedArr as $data) {
    if(in_array($data,$arr))
    {
        $orderedUsers[] = $data;
    }
}

this will output

array (size=4)
  0 => string 'john' (length=4)
  1 => string 'kevien' (length=6)
  2 => string 'smith' (length=5)
  3 => string 'willion' (length=7)

Live Demo

Comments

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.