add to array if it isn't there already

前端 未结 14 1093
北海茫月
北海茫月 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:55

    you can try using "in_array":

    function insert_value($value, &$_arr) {
        if (!in_array($value, $_arr)) {
            $_arr[] = $value;
        }
    }
    

提交回复
热议问题