简体   繁体   中英

return array containing duplicates php

i have a multidimensional array and i only want to keep the entries with the most duplicates. the closest i got was:

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

but that only works for single dimensional arrays and outputs all duplicates. how would i go about doing this?

examples of desired output:

if the array is:

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

it should return array([1] => ( [u] => test1u [d] => test1d))

and if the array is:

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
    )
)

it should return array([1] => ( [u] => test1u [d] => test1d)[2] => ( [u] => test2u [d] => test2d))

but if the array is:

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
    )
)

it should only return array([1] => ( [u] => test1u [d] => test1d))

EDIT:

there are duplicate entries in the array because the array came from $arr = json_decode($arr); and the original JSON had duplicate entries. if there is a better way to do this without decoding the json, let me know.

this is being used as part of a search program. the JSON is an array of all of the entries from the source array that met the criteria for one of the search terms. keeping the entries with the most duplicates insures that those entries contained most if not all of the search terms.

here is the JSON file being decoded:

[{"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":"--"}]}]

in this case the search that made this JSON was for "roses daffodil"

the second example it add – each index just can appear once. for the first this should work fine:

<?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 );

?>

unfortunately array_count_values only works for String and int, so it does not work when you have complex values.

First you array cannot have same key. Check the 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);

i have found the solution! bit long winded but it works!

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM