Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using the following loop to add items to an an array of mine called $liste. I would like to know if it is possible somehow not to add $value to the $liste array if the value is already in the array? Hope I am clear. Thank you in advance.

$liste = array();
foreach($something as $value){
     array_push($liste, $value);
}
share|improve this question

5 Answers 5

up vote 21 down vote accepted

You check if it's there, using in_array, before pushing.

foreach($something as $value){
    if(!in_array($value, $liste, true)){
        array_push($liste, $value);
    }
}

The ,true enables "strict checking". This compares elements using === instead of ==.

share|improve this answer
    
You rock Rocket :) Thanks a lot... –  Marc Apr 10 '12 at 20:33
    
You're welcome :-) –  Rocket Hazmat Apr 10 '12 at 20:33
2  
Be aware that, if $value is numeric and happens to be the same as one of the (numeric) keys in $liste, in_array() can return true even if no such value is in the array. I suggest using array_search() instead and strong-comparing the result against false. –  Jazz Apr 10 '12 at 20:37
1  
@Jazz: What are you talking about? in_array only searches array values, not keys. ideone.com/JjkuP –  Rocket Hazmat Apr 10 '12 at 20:51
1  
@Rocket, I went back and looked. You are correct that I am mischaracterizing the issue with in_array() -- it does not have issues with numeric keys, but with loose type conversion. My mistake! In the code where I used it before, using strict mode was not an option (I needed '2' to compare the same as 2 but not the same as '2thing'). I still recommend that new PHP developers use array_search() if only because it has fewer "gotcha's" than in_array(). The moral is -- whatever you use, make sure you know when it won't work. –  Jazz Apr 10 '12 at 22:46

maybe you want to use it as an associative array instead. it's implemented as (something like) a hash table, so you get constant insert time instead of linear.

function find_uniq( $something ) {
    foreach($something as $value){
         $liste[$value]++;
    }
    return array_keys( $liste );
}
share|improve this answer
    
+1 for suggesting an alternate solution to the problem :-) –  Rocket Hazmat Apr 10 '12 at 21:11

Two options really.

Option 1: Check for each item and don't push if the item is there. Basically what you're asking for:

foreach($something as $value) {
    if( !in_array($value,$liste)) array_push($liste,$value);
}

Option 2: Add them anyway and strip duplicates after:

foreach($something as $value) {
    array_push($liste,$value);
}
$liste = array_unique($liste);

By the look of it though, you may just be looking for $liste = array_unique($something);.

share|improve this answer
1  
Thank you very much Kolink for proposing different alternatives... –  Marc Apr 10 '12 at 20:34

You can simply check this condition before calling array_push(). Use array_search() and use a strong comparison to false to see if the value is present:

foreach( $something as $value ){
    if( array_search( $value, $liste, true ) === false ){
        array_push( $liste, $value );
    }
}

(By the way: Add ,true to array_search to use "strict checking". This will use === for comparisons instead of ==)

share|improve this answer
    
Thank you Jazz :) –  Marc Apr 10 '12 at 20:41
    
I don't know where you heard that info about in_array, but it's completely wrong. in_array only searches values, not keys. ideone.com/JjkuP –  Rocket Hazmat Apr 10 '12 at 20:58
    
@Rocket, I see your results, but in the past I have received different results. It may be that the issue was fixed before 5.2.11. –  Jazz Apr 10 '12 at 22:30
    
@Jazz: You must have done something wrong in your code, that was never an issue, not even in PHP 4. –  Rocket Hazmat Apr 10 '12 at 22:37
    
@Rocket, See this link or this link for people who have experienced issues with in_array(). I can assure you that when I encountered this issue about three years ago, I verified quite thoroughly that the issue was not in my code. –  Jazz Apr 10 '12 at 22:41

Great answers are already present above, but if you have multiple array_push() all over your code, it would be a pain to write if(in_array()) statements every time.

Here's a solution that will shorten your code for that case: Use a separate function.

function arr_inserter($arr,$add){ //to prevent duplicate when inserting
    if(!in_array($add,$arr))
        array_push($arr,$add);
    return $arr;
}

Then in all your array_push() needs, you can call that function above, like this:

$liste = array();
foreach($something as $value){
    $liste = arr_inserter($liste, $value);
}

If $value is already present, $liste remains untouched.

If $value is not yet present, it is added to $liste.

Hope that helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.