从数组中删除重复值并选择一个PHP

时间:2018-03-26 08:58:46

标签: php

Array
(
    [0] => Array
        (
            [Access ID] => 12345
            [Registration Date] => 2018-02-27
            [First Name] => Damian
            [Last Name] => Martin
            [Flying Tour] => 
        )

    [1] => Array
        (
            [Access ID] => 12345
            [Registration Date] => 2018-02-27
            [First Name] => Damian
            [Last Name] => Martin
            [Flying Tour] => Yes going
        )

    [2] => Array
        (
            [Access ID] => 789456
            [Registration Date] => 2018-03-27
            [First Name] => Ricky
            [Last Name] => Smith
            [Flying Tour] => 
        )

    [3] => Array
        (
             [Access ID] => 789456
            [Registration Date] => 2018-03-27
            [First Name] => Ricky
            [Last Name] => Smith
            [Flying Tour] => Two way going
        )

    [4] => Array
        (
            [Access ID] => 987654
            [Registration Date] => 2018-04-27
            [First Name] => Darron
            [Last Name] => Butt
            [Flying Tour] => 
        )

)

$results = [];
      foreach($data as $input){

      $isDuplicate = false;
      foreach($results as $result){
        if(
            strtolower($input['First Name'])===strtolower($result['First Name']) &&
            strtolower($input['Last Name'])===strtolower($result['Last Name'])      &&
            strtolower($input['Registration ID'])===strtolower($result['Registration ID']) &&
            strtolower(!empty($input['Flying Tour']))
        ){
            //a duplicate was found in results
            $isDuplicate = true;
            break;
        }
      }
      //if no duplicate was found
      if(!$isDuplicate) $results[]=$input;
}

查找重复值并移除一个并选择一个。除了飞行巡视条件添加不能返回正确的输出外,它确实适用于所有人。

这应该归还。我不知道我哪里出错了,请再次指导谢谢 哦,我希望这应该是

Array
(
    [0] => Array
        (
            [Access ID] => 12345
            [Registration Date] => 2018-02-27
            [First Name] => Damian
            [Last Name] => Martin
            [Flying Tour] => Yes going
        )

    [1] => Array
        (
            [Access ID] => 789456
            [Registration Date] => 2018-03-27
            [First Name] => Ricky
            [Last Name] => Smith
            [Flying Tour] => Two way going
        )

    [2] => Array
        (
            [Access ID] => 987654
            [Registration Date] => 2018-04-27
            [First Name] => Darron
            [Last Name] => Butt
            [Flying Tour] => 
        )

)

请进行一些更改,请参阅

6 个答案:

答案 0 :(得分:0)

这里的要点是,对于第一次运行,$results仍为空。因此,可以设置$isDuplicate = true的整个逻辑永远不会为第一个实例运行。

这意味着对于第一条记录$isDuplicate = false,因此它将始终被添加。

此外,strtolower(!empty($input['Flying Tour']))有点奇怪......我认为应该给出错误(即使它没有...)

您应该将!empty($input['Flying Tour'])移到for循环之外,因为它不需要新数组中的当前值来进行检查。这将解决问题。

if(!$isDuplicate && !empty($input['Flying Tour'])) $results[]=$input;

如果您想学习和改进代码,请查看array_filter为此目的:过滤数组结果。

- 修改

foreach($results as $id => $result){
    if(
        strtolower($input['First Name'])===strtolower($result['First Name']) &&
        strtolower($input['Last Name'])===strtolower($result['Last Name'])      &&
        strtolower($input['Registration ID'])===strtolower($result['Registration ID']))
    ){
        // Check if the $results value for Flying Tour is empty
        // if yes: update its value $results[ $id ]['Flying Tour'] = $input['Flying Tour'], but still mark as duplicate(!)

        //a duplicate was found in results
        $isDuplicate = true;
        break;
    }
  }

答案 1 :(得分:0)

使用foreach()和数组键检查重复项: -

$results = [];
foreach($data as $input){
     if(!isset($results[$input['Access ID'].'_'.$input['First Name'].'_'.$input['Last Name']])){
            $results[$input['Access ID'].'_'.$input['First Name'].'_'.$input['Last Name']] = $input;
     }else{
       if($results[$input['Access ID'].'_'.$input['First Name'].'_'.$input['Last Name']]['Flying Tour'] ==''){
            $results[$input['Access ID'].'_'.$input['First Name'].'_'.$input['Last Name']] = $input;
       }
     }
}

$results = array_values($results);
//array_multisort( array_column($results, "First Name"), SORT_ASC, $results );
echo "<pre/>";print_r($results);

输出: - https://eval.in/978636https://eval.in/978632

答案 2 :(得分:0)

从应该唯一的值中创建一个键。

如果$results中每个循环数组中的组合键不存在,请将其包含在结果中。

此外,如果组合键存在且Flying Tour值为空,则替换为新数组。

$results = [];

foreach ($data as $input) {
    $key = implode([
        $input['First Name'], 
        $input['Last Name'], 
        $input['Access ID']
    ], '-');

    if (array_key_exists($key, $results) 
        && !empty($results[$key]['Flying Tour'])) continue;

    $results[$key] = $input;
}

var_dump(array_values($results));

答案 3 :(得分:0)

使用MessageBox迭代Private Sub UserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load If Not Site Is Nothing AndAlso Site.DesignMode Then If Not ParentForm.FormBorderStyle = FormBorderStyle.None Then MessageBox.Show("This control works better with a borderless form!!!", "Control Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End If End Sub 。通过foreach变量存储重复信息,并使用它来删除重复值。

<强>替代:

$data

答案 4 :(得分:0)

你可以试试这个:

<?php

$data = Array
(
    '0' => Array
        (
            'Access ID' => 12345,
            'Registration Date' => '2018-02-27',
            'First Name' => 'Damian',
            'Last Name' => 'Martin',
            'Flying Tour' => ''
        ),

    '1' => Array
        (
            'Access ID' => 12345,
            'Registration Date' => '2018-02-27',
            'First Name' => 'Damian',
            'Last Name' => 'Martin',
            'Flying Tour' => 'Yes going'
        ),

    '2' => Array
        (
            'Access ID' => 789456,
            'Registration Date' => '2018-03-27',
            'First Name' => 'Ricky',
            'Last Name' => 'Smith',
            'Flying Tour' => ''
        ),

    '3' => Array
        (
             'Access ID' => 789456,
            'Registration Date' => '2018-03-27',
            'First Name' => 'Ricky',
            'Last Name' => 'Smith',
            'Flying Tour' => 'Two way going',
        ),

    '4' => Array
        (
            'Access ID' => 987654,
            'Registration Date' => '2018-04-27',
            'First Name' => 'Darron',
            'Last Name' => 'Butt',
            'Flying Tour' => ''
        )

);
echo '<pre>';
$data = array_reverse($data);
$combined = array_map(function($row){

    $row = array_map('trim', $row);
    unset($row['Flying Tour']);
    //print_r($row);exit;
    return array_reduce($row, function($v1,$v2){
        return $v1 . "|" . $v2;
    });
}, $data);

$uniqueElements  = array_unique($combined);

$uniqueIndexes = array_keys($uniqueElements);
$newData = [];

foreach ($data as $key => $value) {
    if(in_array($key, $uniqueIndexes)){
        $newData[] = $value;
    }
}

print_r($newData);
?>

供参考:https://eval.in/978642

答案 5 :(得分:0)

希望这会对你有所帮助。

        <?php

        $res = Array
        (
            "0" => Array
                (
                    "Access ID" => 12345,
                    "Registration Date" => 2018-02-27,
                    "First Name" => Damian,
                    "Last Name" => Martin,
                    "Flying Tour" => "",
                ),

            "1" => Array
                (
                    "Access ID" => 12345,
                    "Registration Date" => 2018-02-27,
                    "First Name" => Damian,
                    "Last Name" => Martin,
                    "Flying Tour" => Yesgoing,
                ),

            "2" => Array
                (
                    "Access ID" => 789456,
                    "Registration Date" => 2018-03-27,
                    "First Name" => Ricky,
                    "Last Name" => Smith,
                    "Flying Tour" => "",
                ),

            "3" => Array
                (
                     "Access ID" => 789456,
                    "Registration Date" => 2018-03-27,
                    "First Name" => Ricky,
                    "Last Name" => Smith,
                    "Flying Tour" => Twowaygoing,
                ),

            "4" => Array
                (
                    "Access ID" => 987654,
                    "Registration Date" => 2018-04-27,
                    "First Name" => Darron,
                    "Last Name" => Butt,
                    "Flying Tour" => "",
                )

        );
        $arrayvalues = array();   // Set the array before foreach 

        foreach( $res as $key=>$value ) {
            if( isset( $arrayvalues[$value['Access ID']] ) ) {   // Here checking the Array value and $value are same
                unset( $res[$key] );            // unset the array key value here
            }
            else $arrayvalues[$value['Access ID']] = $value['Access ID'];      // If it is not it will display here and returns true.
        }


                    $i=0;
         $testvalues = array();   // Set the array before foreach 
        foreach ($res as $key => $value) {

            print_r($value);



            $arr[$i] = $value;


            $testvalues[$i]=$arr[$i];

             // unset($arr[$key]);
            $i++;

        }

       echo "<pre>";
        print_r($testvalues) ;   // Get the respective out put here
        echo "</pre>";


    ?>

由于