schizoman Posted December 14, 2011 Share Posted December 14, 2011 Okay, I'm going nuts here. I have a (nested) array of search terms that I'm working with. When I run a foreach loop on it, things go bad. Here's a test loop I ran: echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; foreach($pqPhrases as $pqPhrase) { echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; } Output (emphasis added): doberman - camaro - little green buddha with pink nose doberman - camaro - doberman doberman - camaro - camaro doberman - camaro - camaro If I switch to a "for" loop, the results are as expected: echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; for($x=0;$x<count($pqPhrases);++$x) { echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; } Output: doberman - camaro - little green buddha with pink nose doberman - camaro - little green buddha with pink nose doberman - camaro - little green buddha with pink nose doberman - camaro - little green buddha with pink nose The other thing I tried was to redefine a simplified array, which also gives results that are expected: $pqPhrases=array(); $pqPhrases[0]["terms"]="camaro"; $pqPhrases[1]["terms"]="doberman"; $pqPhrases[2]["terms"]="little green buddha with pink nose"; echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; foreach($pqPhrases as $pqPhrase) { echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; } Output: doberman - camaro - little green buddha with pink nose doberman - camaro - little green buddha with pink nose doberman - camaro - little green buddha with pink nose doberman - camaro - little green buddha with pink nose I'm thoroughly flumoxed. I can't figure out why the first example doesn't work. Am I missing something obvious here? Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/ Share on other sites More sharing options...
KevinM1 Posted December 14, 2011 Share Posted December 14, 2011 $pqPhrases is NOT the same thing as $pqPhrase.... Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/#findComment-1297730 Share on other sites More sharing options...
schizoman Posted December 14, 2011 Author Share Posted December 14, 2011 No, it's not. I'm echoing the contents of $pqPhrases within the loops to illustrate how the contents are being changed in the original array. Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/#findComment-1297734 Share on other sites More sharing options...
QuickOldCar Posted December 14, 2011 Share Posted December 14, 2011 Just as KevinM1 stated. in your first foreach foreach($pqPhrases as $pqPhrase) your supposed to echo $pqPhrase, not $pqPhrases Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/#findComment-1297738 Share on other sites More sharing options...
Drummin Posted December 14, 2011 Share Posted December 14, 2011 I couldn't get example one using $pgPhrases to NOT work. Seems fine to me, where changing the echo to $pgPhrase give array offset error. <?PHP $pqPhrases=array(); $pqPhrases[0]["terms"]="camaro"; $pqPhrases[1]["terms"]="doberman"; $pqPhrases[2]["terms"]="little green buddha with pink nose"; echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; foreach($pqPhrases as $pqPhrase) { echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/#findComment-1297751 Share on other sites More sharing options...
KevinM1 Posted December 14, 2011 Share Posted December 14, 2011 As for why the first example doesn't work, it likely has to do with the way foreach works internally. It's probably getting messed up due to where its internal iterator pointer is in the collection. That said, you're not doing anything useful with your canned example anyway since you're directly accessing the array elements. If you're going to do that, you don't need a foreach. Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/#findComment-1297811 Share on other sites More sharing options...
MikeDean89 Posted December 14, 2011 Share Posted December 14, 2011 KevinM1 is correct, you're using a foreach loop but you're directly accessing the array elements, makes no sense to use a foreach loop if that's what you intend to do. If you're not, I'd go with the following method. <?php $pqPhrases = $terms = array(); $pqPhrases[]["terms"]="camaro"; $pqPhrases[]["terms"]="doberman"; $pqPhrases[]["terms"]="little green buddha with pink nose"; foreach($pqPhrases as $pqPhrase) { $terms[] = $pqPhrase["terms"]; } echo implode(' - ', $terms); ?> Perhaps you should explain what you're looking to do in a little more detail. Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/#findComment-1297817 Share on other sites More sharing options...
schizoman Posted December 15, 2011 Author Share Posted December 15, 2011 In the original loops, I'm accessing $pqPhrase["terms"] and not $pqPhrases["terms"]. While I was trying to figure out why the last phrase was being dropped, I switched to printing out the terms in $pqPhrases to verify the change wasn't just in the way $pqPhrase was being handled. I will be using $phPhrase, and NOT $pqPhrases in the actual code. I access $pqPhrases in the foreach loop for debugging purposes ONLY. To my point: $pqPhrase should in no way, shape or form alter $pqPhrases. If I wanted to alter the "terms" fields in $pqPhrases, I could use &$pqPhrase. For some reason, the contents of $pqPhrases IS being altered within the foreach loop. Since my OSOMTBUFDB (Offensive, Stupid, Only Meant To Be Used For DeBugging) code is getting in the way, I'll offer this up: echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; foreach($pqPhrases as $pqPhrase) { } echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; Output: camaro - doberman - little green buddha with pink nose camaro - doberman - doberman By the way, MikeDean89 asked for me to post what I'm actually trying to do. Here's a simplified version: $rows=""; $row="<tr><td>Your Search Term: ^term^</td></tr>\n"; foreach($pqPhrases as $pqPhrase) { $rows .= str_replace("^term^",$pqPhrase["terms"],$row); } echo $rows; The output: <tr><td>Your Search Term: camaro</td></tr> <tr><td>Your Search Term: doberman</td></tr> <tr><td>Your Search Term: doberman</td></tr> So, while everyone was obsessing on the hubris of accessing $pqPhrases within my foreach loop, I continued to search for an answer, and found this: http://schlueters.de/blog/archives/141-References-and-foreach.html KevinM1 is absolutely correct when he says It's probably getting messed up due to where its internal iterator pointer is in the collection. I have an earlier foreach loop where I'm sanitizing each term and adding it to a mySQL table. Until I read the article at the above link, I would never have expected the resulting behavior, so didn't mention the previous loop. By adding a line to my code before I execute the foreach loop, the expected results were achieved: echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; unset($pqPhrase); // get rid of the offending pointer foreach($pqPhrases as $pqPhrase) { } echo $pqPhrases[0]["terms"]." - ".$pqPhrases[1]["terms"]." - ".$pqPhrases[2]["terms"]."<br>"; Output: camaro - doberman - little green buddha with pink nose camaro - doberman - little green buddha with pink nose I apologize for any confusion my original post created, but I'd been trying to debug the code for awhile before posting here, and what I was trying to do within the foreach loop seemed completely irrelevant to what the foreach loop was doing to my array. Printing out the contents of $pqPhrases[$x]["terms"] within the foreach loop was meant to illustrate how $pqPhrases was being changed during each iteration. Hope this clears things up, and hope the link helps anyone else who finds their arrays being altered by a seemingly simple foreach. Quote Link to comment https://forums.phpfreaks.com/topic/253136-php-is-eating-my-little-green-buddha-foreach-loop-help-needed/#findComment-1298079 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.