简体   繁体   English

循环遍历多个数组

[英]looping through multiple arrays

i'm really confused about something and need some advice. 我真的很困惑,需要一些建议。 i want to be able to loop through 2 arrays at the same time but i can't seem to figure it out. 我希望能够同时循环2个阵列,但我似乎无法弄明白。

  $query = "SELECT * FROM `table1`" ;
    $result = mysql_query($query) or die(mysql_error());
    $total = mysql_num_rows($result);

    while($row = mysql_fetch_array($result)){
    $ip = $row['ip'];
    $domain = $row['domain'];
    }

    ..... bunch of code using $ip and $domain variables .....

i was going to use foreach, but i can only do 1 array at a time. 我打算使用foreach,但我一次只能做一个数组。

foreach($ip as $aip){
echo "$aip"; // how can i add my $domain array as well? 
}

am i missing something? 我错过了什么吗? how can i use both arrays at the same time? 我怎么能同时使用两个阵列? sorry for the noob question. 对不起这个菜鸟问题。

You have to use $ip and $domain directly inside your while() loop: 你必须你的while()循环中直接使用$ ip和$ domain:

while($row = mysql_fetch_array($result)){
    $ip = $row['ip'];
    $domain = $row['domain'];

    ..... bunch of code using $ip and $domain variables .....
}

No need for another foreach(). 不需要另一个foreach()。

foreach($ip as $key => $aip){
    echo $aip . $domain[$key]; 
}

But this would assume $domain and $ip are actually arrays which from your example does not appear to be the same case (and that they have the same keys and number of elements)... 但是这会假设$ domain和$ ip实际上是数组,从你的例子看起来并不是相同的情况(并且它们具有相同的键和元素数量)......

foreach (array_combine($ip, $domain) as $aip => $adomain)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM