Php 返回包含重复项的数组
我有一个多维数组,我只想保留副本最多的条目。我得到的最接近的结果是:Php 返回包含重复项的数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个多维数组,我只想保留副本最多的条目。我得到的最接近的结果是: $wd = array_unique($arr); $d = array_diff($arr, $wd); print_r($d); 但这只适用于一维数组并输出所有副本。我该怎么做呢 所需输出的示例: 如果阵列为: array( [1] => ( [u] => test1u [d] => test1d ) [2] => (
$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
)
[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
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[1] => (
[u] => test1u
[d] => test1d
)
)
它应该返回数组([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
)
)
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
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[1] => (
[u] => test1u
[d] => test1d
)
)
它应该返回数组([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
)
)
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
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[1] => (
[u] => test1u
[d] => test1d
)
)
它应该只返回数组([1]=>([u]=>test1u[d]=>test1d))
编辑:
数组中存在重复项,因为数组来自$arr=json\u 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的搜索是为了“roses daffodil”它添加的第二个示例–每个索引只能出现一次。 对于第一种情况,这应该很好:
<?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\u count\u values
只对String和int有效,因此当您有复杂的值时,它不起作用。首先,数组不能有相同的键。检查一下
我找到了解决办法!有点冗长,但它的工作
$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);
执行var\u export()
并在可能的情况下更新您的问题,这会很有帮助。一个数组中的键怎么可能重复?当一个数组中有多个相同键的实例时,它们会被覆盖。索引必须是唯一的。