PHP多维数组中的多个重复值



我试图更新下面的数组设置"duplicate" => true时,"name""date"是相同的。在下面的例子中,'array[1][duplicate]=>true'作为两者array[0] & array[1]有相同的"name"=john & "date"=2015-7-24

Array
(
    [0] => Array
    (
        [id] => 1
        [name] => john
        [date] => 2015-07-24
        [duplicate] => false
    )
    [1] => Array
    (
        [id] => 1
        [name] => john
        [date] => 2015-07-24
        [duplicate] => false
    )
    [2] => Array
    (
        [id] => 1
        [name] => jane
        [date] => 2015-07-24
        [duplicate] => false
    )
    [3] => Array
    (
        [id] => 1
        [name] => notJaneORJohn
        [date] => 2015-07-24
        [duplicate] => false
    )
    [4] => Array
    (
        [id] => 1
        [name] => jane
        [date] => 2099-07-24
        [duplicate] => false
    )
)                    

试试这个,

$array = Array
(
    0 => Array
    (
        'id' => '1',
        'name' => 'john',
        'date' => '2015-07-24',
        'duplicate' => 'false',
    ),
    1 => Array
    (
        'id' => 1,
        'name' => 'john',
        'date' => '2015-07-24',
        'duplicate' => 'false'
    ),
    2 => Array
    (
        'id' => 1,
        'name' => 'jane',
        'date' => '2015-07-24',
        'duplicate' => 'false'
    ),
    3 => Array
    (
        'id' => 1,
        'name' => 'notJaneORJohn',
        'date' => '2015-07-24',
        'duplicate' => 'false'
    ),
    4 => Array
    (
        'id' => 1,
        'name' => 'jane',
        'date' => '2099-07-24',
        'duplicate' => 'false'
    )
);
foreach ($array as $key => $value) {
    for ($i = $key + 1 ; $i < sizeof($array); $i++) {
        if ($value['name'] === $array[$i]['name'] && $value['date'] === $array[$i]['date']) {
            $array[$key]['duplicate'] = 'TRUE';
            $array[$i]['duplicate'] = 'TRUE';
        }
    }
}

这样可以:

$Arr = Array(
        ['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
        ['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
        ['id'=>1, 'name'=>'Jane', 'date'=>'2015-07-24', 'duplicate'=>0]
    );
foreach($Arr as $i1 => $v1){
    $Str1 = $v1['name'].$v1['date'];
    foreach($Arr as $i2 => $v2){
        if( $i1 !== $i2 && $Str1 === $v2['name'].$v2['date'] ){
            $Arr[$i1]['duplicate'] = 1;
        }
    }
}
echo '<pre>',print_r($Arr),'</pre>'; die();

…输出:

Array(
[0] => Array
    (
        [id] => 1
        [name] => john
        [date] => 2015-07-24
        [duplicate] => 1
    )
[1] => Array
    (
        [id] => 1
        [name] => john
        [date] => 2015-07-24
        [duplicate] => 1
    )
[2] => Array
    (
        [id] => 1
        [name] => Jane
        [date] => 2015-07-24
        [duplicate] => 0
    )
)

看看这个快捷方法;)

<?php $testarr = array(
    array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
    array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
    array("id" => 1,"name" => "jane","date" => "2015-07-24","duplicate" => "false"),
    array("id" => 1,"name" => "notJaneORJohn","date" => "2015-07-24","duplicate" => "false"),
    array("id" => 1,"name" => "jane","date" => "2099-07-24","duplicate" => "false")
);
$tempArray = array();
function checkDuplicate(&$arr) {
    global $tempArray;
    if (count($tempArray) > 0 && in_array($arr['name'], $tempArray) && in_array($arr['date'], $tempArray)) {
        $arr['duplicate'] = "true";
    } else {
        $tempArray[] = $arr['name'];
        $tempArray[] = $arr['date'];
    }
}
array_walk($testarr, 'checkDuplicate');
print_r($testarr);

相关内容

  • 没有找到相关文章

最新更新