2016-06-11 83 views
0

下面的代碼刪除非唯一的PHP數組值:從非關聯數組

$ids=['one','two','three','two','four']; 
echo('<pre>'.print_r($ids,1).'</pre>'); 
$ids = array_unique($ids); 
echo('<pre>'.print_r($ids,1).'</pre>'); 

產生如下:

Array 
(
    [0] => one 
    [1] => two 
    [2] => three 
    [3] => two 
    [4] => four 
) 

Array 
(
    [0] => one 
    [1] => two 
    [2] => three 
    [4] => four 
) 

如何保持連續數組索引併產生如下:

Array 
(
    [0] => one 
    [1] => two 
    [2] => three 
    [3] => four 
) 
+1

@PrototypeChain謝謝。對不起,我搜索了但沒有找到它。 – user1032531

+0

這是一個奇怪的問題,作爲'6,852'信譽 – RomanPerekhrest

+0

PrototypeChain第一評論說http://stackoverflow.com/questions/13596128/array-unique-and-then-renumbering-keys是可能重複的,但後來刪除了評論。我不同意http://stackoverflow.com/questions/591094/how-do-you-reindex-an-array-in-php直接回答我的問題,但PrototypeChain的確。 – user1032531

回答

1

再添加一個線 array_values($ids)

+1

當時,我太親近了!謝謝! – user1032531