简体   繁体   English

array_unique没有给出正确的输出

[英]array_unique not giving the correct output

this give results like 这给像这样的结果

Array( [0] => 5000 [1] => 5001 [2] => 3000 [3] => 3001 [4] => 5000 ) 数组([0] => 5000 [1] => 5001 [2] => 3000 [3] => 3001 [4] => 5000)

When I use array_unique same result is obtained 当我使用array_unique时,获得相同的结果

Array ( [0] => 5000 [1] => 5001 [2] => 3000 [3] => 3001 [4] => 5000 ) 数组([0] => 5000 [1] => 5001 [2] => 3000 [3] => 3001 [4] => 5000)

foreach($detail as $key => $value){
            array_push($zips, $value);  
            }   
}

array_unique($zips);

array_unique() does not work by reference. array_unique()不能通过引用工作。 That means, it won't actually modify the original array, just return a new array with the respective modifications. 这意味着,它实际上不会修改原始数组,而只是返回具有相应修改的新数组。 You need to save its output to a new (or the same) variable: 您需要将其输出保存到新的(或相同的)变量中:

foreach($detail as $key => $value){
    array_push($zips, $value);  
}   

$zips = array_unique($zips);

Read more about Passing by Reference 阅读有关通过引用传递的更多信息

不需要foreacharray_push ,只需将其应用并保存到所需的变量$zips

$zips = array_unique(array_values($zips));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM