2013-03-11 132 views
-1

我在php中遇到了一個問題,我想根據它們的鍵值重新索引一個數組的值,以便使每個數組具有相同的順序以便能夠輕鬆地比較它們(刪除重複項),它應該適用於任何長度的數組刪除重複項。例如A,C,B,D/D,B,C,A/C,A,B,D應該全部重新排序以具有值A,B,C,D,因爲給定的列表與他們的索引字符是0-> A,1-> B,2-> C,3-> dPHP - 根據另一個陣列對數組進行排序

例如,讓我們說,我們有此數組

Array(
    [0] => Array (
    [0] => S, 
    [1] => E, 
    [2] => Q, 
    [3] => A 
) 
) 

,我們有一個給定的列表具有索引的字符(具有它們應具有的索引的字符):

// index --> character (array with real indexes) 
$array_real_index = Array(
    [0] => A, 
    [1] => C, 
    [2] => D, 
    [3] => B, 
    [4] => E, 
    [5] => Q, 
    [6] => R, 
    [7] => S 
) 

如果字符找到的索引應該用正確的索引替換(如果不是正確的索引)。

所以結果應該是:

Array (
    [0] => A, 
    [1] => E, 
    [2] => Q, 
    [3] => S 
) 

感謝您的幫助未來。

+0

我覺得* *我重新格式化,沒有任何意義的損失,但你的結構有點混亂(括號和括號匹配)。請做一個通讀,並確保我已正確格式化。 – 2013-03-11 15:40:32

+0

簡單地將這兩個數組排序不夠? – 2013-03-11 15:42:40

+0

你可以搜索SO問題,它被問了很多次。使用'usort'功能或類似的。 – Voitcus 2013-03-11 15:42:45

回答

0

試試這個:

<? 
    $sorted_array = array_intersect($sort_array, $array_real_index); 
    sort($sorted_array); 
    print_r($sorted_array); 
?> 
+0

謝謝,請你解釋一下。 – StringerB 2013-03-11 16:39:28

+0

試過它沒有工作。 – StringerB 2013-03-11 16:47:55

0
$original_array = Array(
    0 => 'S', 
    1 => 'E', 
    2 => 'Q', 
    3 => 'A' 
); 
$array_real_index = Array(
    0 => 'A', 
    1 => 'C', 
    2 => 'D', 
    3 => 'B', 
    4 => 'E', 
    5 => 'Q', 
    6 => 'R', 
    7 => 'S' 
); 

// Custom sorting function passed to usort. $a & $b are two values 
// being compared in terms of how they should be sorted. Results: 
// 1: $a > $b (move $b down one and $a up one) 
// -1: $b > $a (move $a down one and $b up one) 
// 0: $a == $b (no change) 
function sort_using_real_index($a,$b){ 
    // allow access to $array_real_index from within this function 
    global $array_real_index; 

    // retrieve the key indexes of the values. E.g. $a might contain 
    // 'R', array_search would return 6. 
    $aI = array_search($a, $array_real_index); 
    $bI = array_search($b, $array_real_index); 

    // Both the found keys are then compared and returned 
    // (1, -1 and 0 results reflect how $a and $b relate in terms 
    // of sort order within the sorted result) 
    return $aI == $bI 
    ? 0 
    : ($aI > $bI ? 1 : -1); 
} 

// dump out the original array (benchmark) 
var_dump($original_array); 

// sort the original array using the custom function 
usort($original_array, 'sort_using_real_index'); 

// output the sorted result 
var_dump($original_array); 

example

正如有人說,你可以使用usort,並通過使用該鍵從一個數組中的排序功能的比較器的自定義功能。

想要使用usort來演示如何使用您認爲最有用的任何度量標準對數組進行排序。

+0

似乎正在努力的'示例'謝謝。你能用文字解釋你的代碼嗎?因爲有些部分我不明白。謝謝 – StringerB 2013-03-11 16:07:05

+0

@StringerB:查看我更新的內嵌評論答案。 – 2013-03-11 16:36:40

+0

好的謝謝。嘗試但不工作,當我使用函數'sort_using_real_index($ a,$ b){'。我應該給$ a和$ b提供什麼,他們的意思是什麼。 @BradChristie – StringerB 2013-03-11 17:32:55

0
  1. 自定義排序是指usort()系列函數之一。
  2. 自定義過濾器意味着array_filter()

代碼:

$master = array('A', 'C', 'D', 'B', 'E', 'Q', 'R', 'S'); 

function myCompare($a, $b) { 
    global $master; 
    $index_a = array_search($a, $master, TRUE); 
    $index_b = array_search($b, $master, TRUE); 
    if($index_a === false && $index_b === false) { 
    // if neither are in the array, then sort by their normal value 
    return ($a === $b) ? 0 : ($a < $b) ? -1 : 1; 
    } else if($index_a === false) { 
    // if only one is in the array, then it is "greater" than the other 
    return 1; 
    } else if($index_b === false) { 
    return -1; 
    } else { 
    // otherwise the item with the lowest index is the "greater" of the two 
    return ($index_a === $index_b) ? 0 : ($index_a < $index_b) ? -1 : 1; 
    } 
} 

function myFilter($var) { 
    global $master; 
    return in_array($var, $master); 
} 

$input = array('A','B','C','D','E','F','G','H'); 
$input = array_filter($input, 'myFilter'); 
usort($input, 'myCompare'); 
print_r($input); 

/* Output: 
Array 
(
    [0] => A 
    [1] => C 
    [2] => D 
    [3] => B 
    [4] => E 
) */ 
+0

這是什麼意思'返回($ a === $ b)? 0:($ a <$ b)? -1:1;'? – StringerB 2013-03-11 16:26:09

+0

,你爲什麼在你的array_search中有一個master。如果你可以在你的array_search中解釋這個參數,這將會有所幫助。謝謝 – StringerB 2013-03-11 16:27:08

+0

'if($ a === $ b){return 0; } else {if($ a <$ b){return -1; } else {return 1; }}',但使用[ternary運算符](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary)。並且'$ master'變量在代碼塊的頂部被聲明,並且是您的排序順序/過濾器集合。 – Sammitch 2013-03-11 16:33:26

相關問題