1

My first array:

normalArray
(
    [0] => Business Class
    [2] => Economy
    [6] => First Class
)

My sorting array:

sortArray
(
    [0] => Economy
    [1] => Business Class
    [2] => First Class
)

I am trying to get this as my result

resultsArray
(
    [2] => Economy
    [0] => Business Class
    [6] => First Class
)

Note that the key and value needs to follow the correct order. So i would need to sort array by an array while keeping the key to the value.

I have searched around and looked at many different examples.

Thanks

Charles
  • 50,943
  • 13
  • 104
  • 142
tony
  • 305
  • 5
  • 18

5 Answers5

1

Try this asort() or arsort()

Example:

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
--OR--
arsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

The above example will output:

a = orange
d = lemon
b = banana
c = apple

For more sorting function attributes check out this link.

may this help you.

Tony Stark
  • 8,064
  • 8
  • 44
  • 63
0

Try this:

$map = array_flip($sortArray);
uasort($normalArray,function($a,$b) use ($map) {return $map[$a] < $map[$b] ? -1 : 1});
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

I believe what you are looking for is asort.

Supericy
  • 5,866
  • 1
  • 21
  • 25
  • Asort doesnt provide with the same results im looking for, the result order will need to be same as my other array. – tony Feb 07 '13 at 01:37
0

Try this arsort()

Example:

 normalArray
(
    [0] => Economy
    [2] => Business Class
    [6] => First Class
)

The above example will output:

resultsArray
(
    [2] => Business Class
    [0] => Economy
    [6] => First Class
)
rohitarora
  • 1,380
  • 2
  • 13
  • 20
  • Doing a arsort or asort doesnt provide the result im asking for. The order will need to be same as my sorting array. Not just checking by alphabetical. Thanks resultsArray ( [2] => Economy [0] => Business Class [6] => First Class ) – tony Feb 07 '13 at 01:35
0
asort($normalArray);

this maintains the index of the array.

ref: http://php.net/manual/en/function.sort.php

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73