john
john

Reputation: 1330

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.

Upvotes: 1

Views: 857

Answers (3)

bungdito
bungdito

Reputation: 3620

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

Upvotes: 0

BenMorel
BenMorel

Reputation: 36642

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().

Upvotes: 3

Brett Zamir
Brett Zamir

Reputation: 14375

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)...

Upvotes: 1

Related Questions