php remove duplicates from array

后端 未结 5 1032
余生分开走
余生分开走 2020-12-20 23:30

I was wondering if anyone could help me out, I\'m trying to find a script that will check my entire array and remove any duplicates if required, then spit out the array in t

5条回答
  •  情歌与酒
    2020-12-20 23:48

    Try this, it works with large arrays:

    $original = array('one', 'two', 'three', 'four', 'five', 'two', 'four');
    $filtered = array();
    foreach ($original as $key => $value){
        if(in_array($value, $filtered)){
            continue;
        }
        array_push($filtered, $value);
    }
    
    print_r($filtered);
    

    Outputs:

    Array
    (
        [0] => one
        [1] => two
        [2] => three
        [3] => four
        [4] => five
    )
    

提交回复
热议问题