我有一个多维数组,我只想保留最重复的条目。我得到的最接近的是:
$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 :(得分:0)
它添加的第二个例子 - 每个索引只能出现一次。 对于第一个这应该工作正常:
// <Name>Complex method must be covered by tests</Name>
warnif count > 0
from m in Application.Methods
where m.PercentageCoverage < 80 &&
m.CyclomaticComplexity > 10select new {
m,
m.PercentageCoverage,
m.CyclomaticComplexity,
m.NbLinesOfCodeNotCovered,
Debt = (10 + 3*(m.CyclomaticComplexity -10) + 4*m.NbLinesOfCodeNotCovered)
.ToMinutes().ToDebt(),
AnnualInterest = (20 + 2*(m.CyclomaticComplexity) + 6*m.NbLinesOfCodeNotCovered)
.ToMinutes().ToAnnualInterest()
}
不幸的是<?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 );
?>
仅适用于String和int,所以当你有复杂的值时它不起作用。
答案 1 :(得分: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);
答案 2 :(得分: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);