2

It shouldn't be allowed same emails within array:

[
 ["first" => "John", "last" => "Snow","email" => "[email protected]"],
 ["first" => "Sansa","last" => "Stark","email" => "[email protected]"],
 ["first" => "Rob","last" => "Stark","email" => "[email protected]"]
]

There can be unlimited number of elements of array type within outer array. Should I iterate with foreach and have some temp variable in which I am going to write email or is there a more efficient way?

5
  • What is the expected output and what are the different cases possible? Commented Jun 10, 2019 at 10:17
  • you want to remove first john or rob john? Commented Jun 10, 2019 at 10:18
  • I just want to display error that two same emails are in array Commented Jun 10, 2019 at 10:19
  • @AlivetoDie it is being created using javascript, and sent back to server, where I have to json decode it and then check for double values, and return true/false. That's why I can't do anything at the time of creation. Commented Jun 10, 2019 at 10:21
  • 1
    You need JS validation, but better to have it on server side as well. Commented Jun 10, 2019 at 10:24

1 Answer 1

3

You can simple check that the count of the original array elements isn't equal to the count of the unique email addresses (done using array_column() and array_unique()).

if ( count($data) != count(array_unique(array_column($data, "email"))) )    {
    echo "Multiple";
}

(Assuming array is in $data).

Or for just true/false...

echo count($data) != count(array_unique(array_column($data, "email")));
Sign up to request clarification or add additional context in comments.

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.