PHP Removing duplicate objects from array

后端 未结 2 850
面向向阳花
面向向阳花 2020-12-18 11:50

I\'ve tried all sorts of PHP logic and PHP\'s built in functions to remove duplicate values but it is not working no errors shows up but all my JQuery and CSS doesn\'t work

2条回答
  •  无人及你
    2020-12-18 12:29

    try the following code

    function my_array_unique($array, $keep_key_assoc = false){
        $duplicate_keys = array();
        $tmp = array();       
    
        foreach ($array as $key => $val){
            // convert objects to arrays, in_array() does not support objects
            if (is_object($val))
                $val = (array)$val;
    
            if (!in_array($val, $tmp))
                $tmp[] = $val;
            else
                $duplicate_keys[] = $key;
        }
    
        foreach ($duplicate_keys as $key)
            unset($array[$key]);
    
        return $keep_key_assoc ? $array : array_values($array);
    }
    

提交回复
热议问题