Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to loop an array variable in a for-each loop or any other way you suggest . and when looping if an array has two or more identical items/values the loop should skip the data and continue in with the next data .

$users = array ( array('user1', id , email),
                 array('user2', id , email),
                 array('user3', id , email),
                 array('user4', id , email)

foreach ($users as $key) {

  // do something for each user
  // if from arrays user1 , user2 , user3 , user4 there are some identical data .. 
  // skip that user and continue with another user 
  // then return results and number of users skipped          
}

Example : if in array $users user1 has an email address/id same as user3 skip user3 and continue with user4

Hope you have got my point as I'm not so good in English

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
185 views
Welcome To Ask or Share your Answers For Others

1 Answer

One simple way would be to just put all the email addresses that came up so far while looping through the array into another array, and check if the current email is in that already:

$email_addresses_processed_so_far = array();
foreach ($users as $key) {
  if(in_array($key[2], $email_addresses_processed_so_far)) {
    continue;
  }
  $email_addresses_processed_so_far[] = $key[2]; // put email address into array
  // additional processing here
  // …
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...