1

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.

2
  • which two arrays do you mean? Do they have the same size? Commented May 19, 2011 at 11:13
  • Well in this case you don't need a foreach as far as I can tell, you're already looping through the entire array you get from the mysql query. I'm just confused about where the second loop comes from that you want to do? And what's wrong with doing 1 array at a time? You don't have to, but it's easier to just loop through one at a time like you're already doing. So, I'm still not sure what you want exactly. Commented May 19, 2011 at 11:14

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.