在foreach循环中跳过多维数组PHP [英] Skipping a multidimensional Array in a foreach loop PHP

查看:105
本文介绍了在foreach循环中跳过多维数组PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以for-each循环或您建议的任何其他方式循环数组变量.当数组具有两个或多个相同的项/值时,循环应跳过该数据并继续下一个数据.

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          
}

示例: 如果数组$ users中user1的电子邮件地址/id与user3相同,请跳过user3并继续使用user4

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

推荐答案

一种简单的方法是将到目前为止出现的所有电子邮件地址放入该数组中,并将其循环到另一个数组中,并检查当前电子邮件是否为在那已经:

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
  // …
}

这篇关于在foreach循环中跳过多维数组PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆