add to array if it isn't there already

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

    You'd have to check each value against in_array:

    $a=array();
    // organize the array by cusip
    foreach($array as $k=>$v){
        foreach($v as $key=>$value){
            if(!in_array($value, $a)){
            $a[]=$value;
            }
        }
    }
    

提交回复
热议问题