删除重复项并将具有重复项的那些移动到多维数组php的开头

时间:2015-07-09 15:23:14

标签: php arrays array-unique

我正在为csv创建搜索功能。根据我的搜索结果,我想删除重复项,然后将那些具有重复项的项移到数组的开头。

  Array
    (
        [0] => Array
        (
            [0] => COD 21
            [1] => 4567
            [2] => Robert Wahl
        )
    [1] => Array
        (
            [0] => RT 5
            [1] => 1234
            [2] => Robert Merrill
        )
    [2] => Array
        (
            [0] => XD 62
            [1] => 1653
            [2] => Robert Hill
        )
    [3] => Array
        (
            [0] => RT 5
            [1] => 1234
            [2] => Robert Merrill
        )
)

我有一个删除重复项的函数,但是我不知道如何让它将具有重复项的值移动到数组的开头,并根据具有最多重复项的值对它们进行排序:

function arrayUnique($array) { 
    $aux_ini=array(); 
    $result=array(); 
    for($n=0;$n<count($array);$n++) 
    { 
        $aux_ini[]=serialize($array[$n]); 
    } 
    $mat=array_unique($aux_ini); 
    for($n=0;$n<count($array);$n++) 
    { 
        $result[]=unserialize($mat[$n]); 
    } 
    return $result; 
}

感谢任何和所有帮助。

4 个答案:

答案 0 :(得分:3)

function arrayUnique($array) { 
    // count values
    $arr = array_count_values (array_map('serialize',$array)); 
    // sort in desc order
    arsort($arr);
    // take keys and restore array
    return array_map('unserialize',array_keys($arr));
}

答案 1 :(得分:2)

function array_unique_ordered_by_duplicates($array)
{
  //serialize inner arrays
  $serialized = array_map('serialize', $array);
  //create an array indexed by the serialized values, with all keys having a value of 0
  $flipped = array_map(function($el){ 
        return 0; 
      },array_flip($serialized));
  //increment the count each time an element is encountered, thereby gicing duplicates a higher value
  foreach($serialized as $v){
    $flipped[$v]+=1;
  }
  //sort descending on the calculated value, putting the most duplicated items first
  arsort($flipped);
  //return unserialized, deduped, orded array
  return array_map('unserialize',array_keys($flipped));

}

请注意,使用php array_count_values,splash58是上述版本的更高效版本,应该使用

答案 2 :(得分:1)

有点时髦,但我认为代码相对容易理解。

$csv = array(array('COD 21','4567','Robert Wahl')
            ,array('RT 5','1234','Robert Merrill')
            ,array('XD 62','1653','Robert Hill')
            ,array('RT 5','1234','Robert Merrill')
            );

echo "Begin...<br>\n";

$serials = array();
$dupeqty = array();
foreach ($csv as $csvkey => $csvdata)
{
  $dupekey = serialize($csvdata);

  if ( !isset($serials[$dupekey]) )
    $serials[$dupekey] = $csvkey;

  if ( !isset($dupeqty[$dupekey]) )
    $dupeqty[$dupekey] = 0;
  $dupeqty[$dupekey]++;
}

arsort($dupeqty);
foreach ($dupeqty as $dupekey => $times)
  echo $dupekey . " occurs $times times<br>\n";

flush();

echo "<br>\n";
echo "Accessing the original array...<br>\n";
foreach ($dupeqty as $dupekey => $times)
{
  $data = $csv[$serials[$dupekey]];
  echo '... data ' . $data[0] . ' ' . $data[1] . ' ' . $data[2] . "<br>\n";
}

运行它会生成:

Begin...
a:3:{i:0;s:4:"RT 5";i:1;s:4:"1234";i:2;s:14:"Robert Merrill";} occurs 2 times
a:3:{i:0;s:5:"XD 62";i:1;s:4:"1653";i:2;s:11:"Robert Hill";} occurs 1 times
a:3:{i:0;s:6:"COD 21";i:1;s:4:"4567";i:2;s:11:"Robert Wahl";} occurs 1 times

Accessing the original array...
... data RT 5 1234 Robert Merrill
... data XD 62 1653 Robert Hill
... data COD 21 4567 Robert Wahl

答案 3 :(得分:0)

试试这个,

function array_multidimensional($array)
{
$serialized = array_map('serialize', $array);
$array = array_unique($serialized);
return array_intersect_key($array, $unique);
}