1
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;
}

Finding duplicate value and remove one of them and pick one. It does work for all except flying tour condition adding that does not return right output.

This should return this. I don't know where i'm going wrong please guide thank you once again Oh here what i expect this should be

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

)

Some changes are made please see

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Saurav
  • 27
  • 5

6 Answers6

0

The point here is that for the first run, $results is still empty. Thus, the whole logic that can set $isDuplicate = true never runs for the first instance.

This means that for the first record, $isDuplicate = false and thus it will always be added.

Furthermore, strtolower(!empty($input['Flying Tour'])) is a bit weird... and should give an error in my opinion (even if it doesnt...)

You should move the !empty($input['Flying Tour']) outside the for loop, since it does not need the current values in the new array for its check. This will solve the problem.

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

If you want to learn and improve your code, have a look at array_filter it is made for this purpose: filtering array results.

--edit

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;
    }
  }
Jeffrey
  • 1,707
  • 2
  • 25
  • 40
  • if(!$isDuplicate && !empty($input['Flying Tour'])) $results[]=$input; was good but what happend it return only those value that having Flying Tour – Saurav Mar 26 '18 at 09:17
  • if there is no value it should return those value too – Saurav Mar 26 '18 at 09:18
  • ah, now I get it. What you want is to check on the 3 values, if they are the same as the one in $results already (you got that part). Then, you also want to check if the value in `$results` currently has no value for `Flying Tour`. If that is the case, you want to update that record with the new value in `$input`. Though, still mark that record as duplicate, else it will show up twice... – Jeffrey Mar 26 '18 at 09:36
  • Thank you all specially @Oluwafemi Sule – Saurav Mar 26 '18 at 10:02
0

Use foreach() along with array keys to check for duplicates:-

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

Output:-https://eval.in/978636 And https://eval.in/978632

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
0

Compose a key from values that should be unique.

If the composed key from each loop array doesn't exists in $results include it in the results.

Also if composed key exists and Flying Tour value is empty replace with new array.

$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));
Oluwafemi Sule
  • 32,168
  • 1
  • 47
  • 72
0

Uses foreach to iterate over $data. Storing duplicates info via $duplicates variable, and using it to remove duplicate values.

Alternative:

$duplicates = array();
$results = $data;

foreach( $results as $k=>$v ) {
    if( isset( $duplicates[$v['Access ID']] ) ) {
        unset( $results[$k] );
    }
    else $duplicates[$v['Access ID']] = TRUE;
}

print_r( $results );
Karlo Kokkak
  • 3,632
  • 4
  • 17
  • 31
0

You can try this :

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

For reference : https://eval.in/978642

Mihir Bhende
  • 7,846
  • 1
  • 24
  • 34
0

Hope this will help you.

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


    ?>

Thanks

Muthusamy
  • 286
  • 1
  • 11