add to array if it isn't there already

前端 未结 14 1248
北海茫月
北海茫月 2020-12-13 07:53

How do I add elements to an array only if they aren\'t in there already? I have the following:

$a=array();
// organize the array
foreach($array as $k=>$v)         


        
14条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 08:32

    Try adding as key instead of value:

    Adding an entry

    function addEntry($entry) {
        $this->entries[$entry] = true;
    }
    

    Getting all entries

    function getEntries() {
        return array_keys($this->enties);
    }
    

提交回复
热议问题