0
votes

I'm working on a WordPress plugin that extends Gravity Forms. I need to add some functionality to check for duplicate entries. I'm using GFAPI::get_entries( $form_id, $search_criteria ); to find and list all entries associated with my Gravity form that meet a certain search criteria. Here's a snippet of the code:

// Fetch field_ids, establish search criteria
    $first_id = $last_id = $address_id = 0;
    foreach ( $form['fields'] as $field ) {
        if ( $field->get_input_type() == 'name' ) {
            $first_id = $field->id . '.3';
            $last_id  = $field->id . '.6';
            break;
        }
    }
    foreach ( $form['fields'] as $field ) {
        if ( $field->get_input_type() == 'address' ) {
            $address_id = $field->id . '.1';
            break;
        }
    }
    $search_criteria['field_filters'] = array(
        array( 'key' => $first_id, 'value' => $data['voterdata_FirstName'] ),
        array( 'key' => $last_id, 'value' => $data['voterdata_LastName'] ),
        array( 'key' => $address_id, 'value' => $data['voterdata_VoterAddress'] )
    );
    $search_criteria['status'] = 'active';
    $entries = GFAPI::get_entries( $form_id, $search_criteria );

A var_dump(); of $entries returns all entries for my form that meet the search criteria. If more than one entry exists containing the same first name, last name, and address combination, I want to redirect my users to another page so that they may confirm which entry belongs to them. What is the best approach to checking $entries for duplicates?

Thanks in advance,

2

2 Answers

1
votes

You can use in_array

Example:

<?php
$results = [
    ['firstName' => 'Bob', 'lastName' => 'Dole', 'address' => '123 Test'],
    ['firstName' => 'Jon', 'lastName' => 'Doe', 'address' => '444 Foo'],
    ['firstName' => 'Bob', 'lastName' => 'Dole', 'address' => '333 Test']
];

$test = ['firstName' => 'Bob', 'lastName' => 'Dole', 'address' => '123 Test'];

$test2 = ['firstName' => 'Jon', 'lastName' => 'Dole', 'address' => '999 Test'];

$test3 = ['lastName' => 'Jon', 'firstName' => 'Dole', 'address' => '999 Test'];

var_dump(in_array($test, $results));   // This should match
var_dump(in_array($test2, $results));  // This should not match
var_dump(in_array($test3, $results));  // This should not match

Output:

bool(true)
bool(false)
bool(false)

Edit

Just realized you're already searching... In this case, you can use wp_redirect() if $entries is not empty... I'm assuming you're checking for data BEFORE inserting it, correct? You should if not.

0
votes

I've discovered a solution:

if ( count( array_unique( $entries ) ) < count( $entries ) ) {
        # do something ...
    }

Surely, this isn't the only approach. But hopefully it helps someone!