Skipping a multidimensional Array in a foreach loo

2019-09-21 05:00发布

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

标签: php arrays loops
1条回答
Melony?
2楼-- · 2019-09-21 05:15

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
  // …
}
查看更多
登录 后发表回答