2010-06-23 108 views
0

考慮以下陣列的序列的多維數組:排序由另一個陣列

$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')); 

我想排序這個數組由random_number[0]鍵的值,並基於以下陣列的序列

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

所以最終的排序陣列會是什麼樣子:

$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')); 

任何想法如何實現?

回答

2
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); 
+0

太棒了!它的工作原理與我想要的一樣!謝謝一堆! – Tony 2010-06-23 20:22:29