Himani
Himani

Reputation: 265

array_unique not giving the correct output

this give results like

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

When I use array_unique same result is obtained

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

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

array_unique($zips);

Upvotes: 0

Views: 82

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

No need foreach and array_push, just apply and save to the needed variable $zips:

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

Upvotes: 1

ishegg
ishegg

Reputation: 9927

array_unique() does not work by reference. 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

Upvotes: 6

Related Questions