Jump to content

PHP Is Eating My Little Green Buddha - Foreach Loop Help Needed


schizoman

Recommended Posts

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?

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.