Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When using the foreach loop for two variables in an array, the MySQL query returns the first array variable along with the second during the second loop for some strange reason. Would anyone know what the cause of this is? Does the cache need to be cleared or any variables need to be cleared?

$db_array = array($id1,$id2);
foreach ($db_array as &$db_id) {

//MySQL code here

}
unset($db_id);
share|improve this question
3  
You probably need to show us some code (and the result you get, and what you expect), for us to understand your problem –  Pascal MARTIN Aug 14 '11 at 18:07
1  
Second loop... I only see one here? A better code example might enable us to actually help. –  Ross Snyder Aug 14 '11 at 18:23

1 Answer 1

Move the unset($db_id); at the end of the foreach(), so it would be

$db_array = array($id1,$id2);
foreach ($db_array as &$db_id) {

//MySQL code here

unset($db_id);
}
share|improve this answer
    
Unfortunately this didn't work either. –  Peach Aug 14 '11 at 18:19
    
Is there a reason you have a & with $db_id? (is there something I don't know about php?) –  sman591 Aug 14 '11 at 18:25
1  
It's the pass-by-reference operator in PHP - here it lets you change the array elements as you iterate over them. –  Ross Snyder Aug 14 '11 at 18:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.