php remove duplicates from array

后端 未结 5 991
余生分开走
余生分开走 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
    )
    
    0 讨论(0)
  • 2020-12-20 23:49

    stop using your own array splitting function that causes duplicates and read the php manual page on array_chunk

    0 讨论(0)
  • 2020-12-20 23:56

    My PHP's rusty, but something like this should work:

    function makeUniqueBidArray(&$array)
    {
      $tempArray = array();
      foreach($array as $bidArray) {
        foreach($bidArray as $bid) {
          if (isset($tempArray[$bid->bid]) {
            unset($bid);
          } else {
            $tempArray[$bid->bid] = $bid->name;
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-20 23:59

    Use the array_unique function.

    Here is an implentation of a multi-dimensional array_unique function.

    function super_unique($array)
    {
      $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
    
      foreach ($result as $key => $value)
      {
        if ( is_array($value) )
        {
          $result[$key] = super_unique($value);
        }
      }
    
      return $result;
    }
    

    Not tested, from the comments in the function manual.

    0 讨论(0)
  • 2020-12-21 00:01

    You can remove duplicates from an array by doing:

    $array = array_values(array_unique($array));
    
    0 讨论(0)
提交回复
热议问题