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.
$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($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($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($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)...
foreach (array_combine($ip, $domain) as $aip => $adomain)
精彩评论