views:

39

answers:

1

Consider the following array:

$main_array = array();
$main_array[0] = array('location'=> array('France', 'Germany'), 'random_number'=> array('6520'));
$main_array[1] = array('location'=> array('Italy', 'Switzerland'), 'random_number'=> array('3245'));
$main_array[2] = array('location'=> array('Portugal', 'Spain'), 'random_number'=> array('9981'));
$main_array[3] = array('location'=> array('Turkey', 'Greece'), 'random_number'=> array('1098'));

I want to sort this array by the value of random_number[0] key and based on the sequence of the following array:

$sequenced = array('3245','1098','6520','9981');

So the final sorted array would look like:

$final_array = array();
$final_array[0] = array('location'=> array('Italy', 'Switzerland'), 'random_number'=> array('3245'));
$final_array[1] = array('location'=> array('Turkey', 'Greece'), 'random_number'=> array('1098'));
$final_array[2] = array('location'=> array('France', 'Germany'), 'random_number'=> array('6520'));
$final_array[3] = array('location'=> array('Portugal', 'Spain'), 'random_number'=> array('9981'));

Any idea how that would be achieved?

+1  A: 
function sorter($array, $sequence){
  $tmp = array();
  foreach($sequence as $s){
    foreach($array as $a){
      if($a[random_number][0] == $s){ 
        array_push($tmp, $a);
        break;
      }   
    }   
  }
  return $tmp;
}

$final_array = sorter($main_array, $sequenced);
john
Great! It works exactly as I wanted! Thanks a bunch!
Tony