PHP foreach多维数组上的循环 [英] PHP foreach loop on multi dimensional array

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

问题描述

对于多维数组有一点困难。我缩短了它,但数组看起来像这样

  array(192){
[count] = > int(191)
[0] => array(124){
[11] => string(10)usnchanged
[homemta] => array 2){
[count] => int(1)
[0] => string(206)Some String
}
[12] = > string(7)homemta
[proxyaddresses] =>数组(2){
[count] => int(1)
[0] = > string(46)SMTP:remove=email@email.com
}
}
}
}
pre>

我正在尝试获取将在proxyaddresses下列出的电子邮件地址。我现在正在做的是:

$ p $ for($ i = 0; $ i <$ data [count ; $ i ++){
foreach($ data [$ i] [proxyaddresses] as $ object){
print_r($ object);






这是我所需要的数据,我得到了很多警告的数据,例如

lockquote

注意:未定义的索引:index.php中的proxyaddresses在88行


$ b




<所以我认为它不喜欢的东西。如何根据上面的数组结构正确地做循环?



谢谢

解决方案

这是因为每个循环都不存在proxyaddresses元素。你必须检查是否设置或不要使用php isset()函数来避免警告。

 if(isset($ data [$ i] [proxyaddresses])) {
foreach($ data [$ i] [proxyaddresses]作为$ object){
print_r($ object);
}
}
}


having a bit of difficulty with a multi-dimensional array. I have shortened it, but the array looks like this

array(192) {
    ["count"]=> int(191)
        [0]=>array(124) {
            [11]=>string(10) "usnchanged"
                ["homemta"]=>array(2) {
                  ["count"]=>int(1)
                  [0]=>string(206) "Some String"
                }
            [12]=>string(7) "homemta"
                ["proxyaddresses"]=>array(2) {
                  ["count"]=>int(1)
                  [0]=>string(46) "SMTP:remove=email@email.com"
                }
        }
    }
}

I am trying to get the email addresses which will be listed under proxyaddresses. What I am doing at the moment is the following:

for($i=0; $i<$data["count"]; $i++) {
    foreach($data[$i]["proxyaddresses"] as $object) {
        print_r($object);   
    }
}

This gets me the data I need, but inbetween all the data I get a lot of warnings like

Notice: Undefined index: proxyaddresses in index.php on line 88

Warning: Invalid argument supplied for foreach() in index.php on line 88

So I presume it is not liking something. How would I properly do the loop based on the above array structure?

Thanks

解决方案

It's because proxyaddresses element is not present for each loop. You have to check if it's set or not to avoid warning by using php isset() function.

for($i=0; $i<$data["count"]; $i++) {
  if(isset($data[$i]["proxyaddresses"])){
    foreach($data[$i]["proxyaddresses"] as $object) {
      print_r($object);   
    }
  }
}

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

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