0

I have 2D array that contains sets of last names and birth dates. I'm attempting to look for a last name and birth date combo match within the array, but I'm not too sure how to do that.

So, the array to search would look something like this:

Array ( 
    [0] => Array ( 
        [0] => lastName1 
        [1] => 05/24/1937 
    ) 
    [1] => Array ( 
        [0] => lastName2 
        [1] => 06/05/1932 
    ) 
    [2] => Array ( 
        [0] => lastName3 
        [1] => 03/04/1926 
    ) 
)

My user would supply the search criteria of $lastName and $dateOfBirth So for example, say my user enters $lastName = "lastName2" and $dateOfBirth = "06/05/1932" I would like to search the main array and see if there is an exact match and return true if there is a match or false if there is no match.

In this case it should return true since Array # 1 is an exact match to both of the search criteria given.

So far I know how to search the array for one value at a time but I need to search for both values at the same time since there may be several array items with the same last name or birth date, but matching the combo of both should in most cases only return one result if any at all.

2 Answers 2

1

Just create an array using the user submitted data, and then use in_array() to see if the array exists in that exact form:

$searcher = array(
    array( 'surname1', '05/24/1937' ),
    array( 'surname2', '06/05/1932' ),
    array( 'surname3', '03/04/1926' )
);

$toSearch = array( $lastName, $dateOfBirth );

if( in_array($searcher, $toSearch))
{
    echo 'match found'
}
else
{
    'nothing found';
}

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe it was that easy....lol Thanks for the answer. Works like a charm!
0

Search by only one feature and return all the records that match:

array_filter($arr,function ($item) use ($filter_index,$filter_val) { return $item[$filter_index]==$filter_val; });

Search exact match:

in_array($arr,array($last_name,$date))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.