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

前端 未结 9 1880
无人共我
无人共我 2020-12-29 01:04

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

相关标签:
9条回答
  • 2020-12-29 01:38

    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.

    0 讨论(0)
  • 2020-12-29 01:44

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

    0 讨论(0)
  • 2020-12-29 01:48

    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

    0 讨论(0)
  • 2020-12-29 01:52

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

    0 讨论(0)
  • 2020-12-29 01:52

    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);
    
    0 讨论(0)
  • 2020-12-29 01:53

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

    array_push($array, $new_member);
    $array = array_unique($array);
    
    0 讨论(0)
提交回复
热议问题