php array_push() -> How not to push if the array already contains the value

PhpArraysArray Push

Php Problem Overview


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);
}

Php Solutions


Solution 1 - Php

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 ==.

Solution 2 - Php

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);.

Solution 3 - Php

As this question is using not the best practice code to begin with - I find all the answers here to be over-complicated.

Solving code in question:

Basically what is tried to do in question itself is filtering out repetitions. Better approach:

$liste = array_unique($something);

If adding elements to an array that is not empty:

$liste = array_unique(array_merge($liste, $something));

If you are using array_push():

Unless you are actually using return value of array push you should really use:

$liste[] = $value;

for shorter syntax and minor performance increase

Solution 4 - Php

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 );
}

Solution 5 - Php

The right answer is much simpler. Push everything, but then use array_unique() function:

array_push($array, $new_member);
$array = array_unique($array);

Solution 6 - Php

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 ==)

Solution 7 - Php

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.

Solution 8 - Php

Save logic and improve speed by keeping it logic-less. Just keep overwriting.

$list = array();
foreach ($something as $value) {
  if (!is_object($value) && !is_array($value)) {
    $list[$value] = $value
  }
}
$list = array_values($list);

Solution 9 - Php

I have another solution for you! You can use keys as values And keys will never be duplicated.

$arr = ["A" => true, "B" => true, "C" => true];

$item = "A";
$arr[$item] = true;

Usage:

foreach($arr as $value => $helper){
    echo $value;
}

Set class

OK, Let me write a class for you! The arrays do not allow duplicated items are called sets.

class Set{
    private $countainer;

    public function get(){
        return $this->container;
    }
    public function push($item){
        $this->container[$item] = true;
    }
    public function delete($item){
        unset($this->container[$item]);
    }
}

Note: This will not work for associative arrays and values.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcView Question on Stackoverflow
Solution 1 - PhpRocket HazmatView Answer on Stackoverflow
Solution 2 - PhpNiet the Dark AbsolView Answer on Stackoverflow
Solution 3 - PhpNoOorZ24View Answer on Stackoverflow
Solution 4 - PhpbjelliView Answer on Stackoverflow
Solution 5 - PhpNickView Answer on Stackoverflow
Solution 6 - PhpJazzView Answer on Stackoverflow
Solution 7 - PhpITWitchView Answer on Stackoverflow
Solution 8 - PhpdoublejoshView Answer on Stackoverflow
Solution 9 - PhpAmir FoView Answer on Stackoverflow