0

I have a google form,the data collected from it is stored in a spreadsheet, I then use PHP to get the data from the spreadsheet using the Google API.Link Of the Spreadsheet and Here is the code

<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));


require __DIR__ . '/vendor/autoload.php';

$client = new \Google_Client();
$client->setApplicationName('Google Sheets and PHP');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('Offline');
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheetId = "10Ta70Faby4FnbZ_bhELRRPNJ4W8m6rrhdPj5lX0ijZ8";
$range = "sfi!A2:D31";
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if(empty($values)) {
    print "You suck";
}

    
foreach($values as $row) {
    $mask = "%s\n%s\n%s\n";
    $mask2 = "%s\n";
    $values2 = sprintf($mask,$row[1], $row[2],$row[3]);
$values3 = sprintf($mask2, $row[3]);
print($values2);
$fp = fopen($t + '.csv', 'a');//opens file in append mode  
 
fwrite($fp, $values2);  

fclose($fp);  

}


?>

I want to know when 2 or more users have the same interests ie same data in row[3] how shall I do so? I tried using similar_text but it didn't work. Any help will be appreciated!

Navvye
  • 31
  • 5
  • You might want to check this [reference](https://stackoverflow.com/questions/13413465/count-of-duplicate-elements-in-an-array-in-php). In addition, do you plan to count the duplicates per each unique interests? or just check if there are >2 duplicate interest in your data as provided in the existing answer of @rummens? Please provide your expected output – Ron M Jul 26 '21 at 19:19

1 Answers1

0

Why are you trying to compare the data? If it is simply to be notified when there is a shared interest among any of your users, you could decide to use an array to store each interest and count the duplicates of that array by using the array_count_values($array) function. An example, copied from this answer:

$interestsArray = [12,43,66,21,56,43,43,78,78,100,43,43,43,21];
$vals = array_count_values($interestsArray);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);

Result:

No. of NON Duplicate Items: 7
Array
(
    [12] => 1
    [43] => 6
    [66] => 1
    [21] => 2
    [56] => 1
    [78] => 2
    [100] => 1
)
Michel Rummens
  • 459
  • 3
  • 9