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