2017-04-27 45 views
0

我有一個多維數組,我只想保留最重複的條目。我得到的最接近的是:返回包含重複數組的數組php

$wd = array_unique($arr); 
$d = array_diff($arr, $wd); 
print_r($d); 

但這隻適用於單維數組並輸出所有重複。我將如何去做這件事?期望的輸出的

例子:

如果陣列是:

array(
    [1] => ( 
     [u] => test1u 
     [d] => test1d 
    ) 
    [2] => ( 
     [u] => test2u 
     [d] => test2d 
    ) 
    [3] => ( 
     [3] => test3u 
     [3] => test3d 
    ) 
    [1] => ( 
     [u] => test1u 
     [d] => test1d 
    ) 
) 

它應該返回array([1] => ([u] => test1u [d] => test1d))

並且如果陣列是:

array(
    [1] => ( 
     [u] => test1u 
     [d] => test1d 
    ) 
    [2] => ( 
     [u] => test2u 
     [d] => test2d 
    ) 
    [3] => ( 
     [3] => test3u 
     [3] => test3d 
    ) 
    [1] => ( 
     [u] => test1u 
     [d] => test1d 
    ) 
    [2] => ( 
     [u] => test2u 
     [d] => test2d 
    ) 
) 

它應該返回array([1] => ([u] => test1u [d] => test1d)[2] => ([u] => test2u [d] => test2d))

但如果數組是:

array(
    [1] => ( 
     [u] => test1u 
     [d] => test1d 
    ) 
    [2] => ( 
     [u] => test2u 
     [d] => test2d 
    ) 
    [3] => ( 
     [3] => test3u 
     [3] => test3d 
    ) 
    [1] => ( 
     [u] => test1u 
     [d] => test1d 
    ) 
    [2] => ( 
     [u] => test2u 
     [d] => test2d 
    ) 
    [1] => ( 
     [u] => test1u 
     [d] => test1d 
    ) 
) 

它應該只返回array([1] => ([u] => test1u [d] => test1d))

編輯:

有數組中的重複條目,因爲數組從$arr = json_decode($arr);來了,原來的JSON有重複條目。 如果有更好的方法來做到這一點而不解碼json,讓我知道。

這被用作搜索程序的一部分。 JSON是源數組中滿足其中一個搜索項條件的所有條目的數組。保持最重複的條目確保這些條目包含大部分(如果不是全部)搜索條件。

這裏是被解碼的JSON文件:

[{"1":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"2":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"5":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"3":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"4":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"5":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"6":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]}] 

在這種情況下,使得這JSON是爲「玫瑰水仙」

+0

如果可能,做一個'var_export()'並更新你的問題會有幫助。 –

+4

怎麼可能是一個具有重複鍵的數組? – hassan

+0

當數組中存在多個相同鍵的實例時,它們將被覆蓋。索引必須是唯一的。 – Qirel

回答

0

我找到了解決方案!有點長囉,但它的作品!

$json = json_decode($json); 
$jsonoutc = $jsonout = ""; 
$arrid = $arrout = $disp = array(); 
foreach ($json as $null => $arr){ 
    foreach ($arr as $key => $null){ 
     $arrid[] = $key; 
    } 
} 
$vals = array_count_values($arrid); 
foreach ($vals as $val => $counted){ 
    if ($counted > $jsonoutc){ 
     $jsonoutc = $counted; 

    } 
} 
foreach ($vals as $val => $counted){ 
    if ($counted == $jsonoutc){ 
     $arrout[] = $val; 

    } 
} 
foreach ($arrout as $null => $val){ 
    foreach ($json as $null => $arr){ 
     foreach ($arr as $key => $list){ 
      if ($key == $val){ 
       $disp[$key] = $list; 
      } 
     } 
    } 
} 
print_r($disp); 
0

第二個例子是添加搜索 - 每個索引纔會出現一次。 第一個這應該很好地工作:

<?php 

$array[] = array('u' => 'test1u', 'd' => 'test1d'); 
$array[] = array('u' => 'test2u', 'd' => 'test2d'); 
$array[] = array('3' => 'test3u', '3' => 'test3d'); 
$array[] = array('u' => 'test1u', 'd' => 'test1d'); 
$array[] = array('u' => 'test2u', 'd' => 'test2d'); 

var_export($array); 
//echo("\n" . array_count_values($array) . "\n"); 

foreach($array as $k => $v){ 
    foreach($array as $ke => $ve){ 
      if($k == $ke) 
       continue; 
      if($v == $ve) { 
       $d[$k]=$v; 
       unset($array[$k]); 
      } 
    } 
} 

var_export($d); 

?> 

不幸array_count_values僅適用於字符串和整型,所以當你有複雜的價值觀這是行不通的。

0

首先你的數組不能有相同的密鑰。檢查live demo

<?php 

$array[] = array('u' => 'test1u', 'd' => 'test1d'); 
$array[] = array('u' => 'test2u', 'd' => 'test2d'); 
$array[] = array('u' => 'test3u', 'd' => 'test3d'); 
$array[] = array('u' => 'test1u', 'd' => 'test1d'); 
$array[] = array('u' => 'test2u', 'd' => 'test2d'); 

$array = array_map(function($v){return implode('-', $v);}, $array); 
$count = array_count_values($array); 
print_r($count); 
arsort($count); 
$max = current($count); 
while(current($count) == $max) 
{ 
    $arr = explode('-', key($count)); 
    $result[] = array('u' => $arr[0], 'd' => $arr[1]); 
    next($count); 
} 
print_r($result);