2016-06-11 53 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