Jump to content

"Use of unidentified constants" with nested foreach() loops--?


Recommended Posts

Relevant Code:

 

<?php
foreach( $Data as $index => $arraydata){
foreach( $Data[$index] as $index2 => $arraydata2){
	foreach( $Data[$index] as $index3 => $arraydata3){
			if($index3!=$index2 {
				if ($Data[$index][index2]["Name"] == $Data[$index][index3]["Name"]){ //THIS IS PROBLEM LINE
					echo "same name!";
		}
	}
}
}
//...
?>

 

I get the following error for that part of code:

Notice: Use of undefined constant index2 - assumed 'index2' in /Users.../

 

Anybody can tell me what is going on?

Can i not use the "as ______" variablie within the statement?

"index2" and "index3" is not defined as a variable, see correction below:

 

<?php
foreach( $Data as $index => $arraydata){
   foreach( $Data[$index] as $index2 => $arraydata2){
      foreach( $Data[$index] as $index3 => $arraydata3){
            if($index3!=$index2) {
               if ($Data[$index][$index2]["Name"] == $Data[$index][$index3]["Name"]){ //THIS IS PROBLEM LINE
                  echo "same name!";
         }
      }
   }
}
//...
?>

 

By the way, this also works (if you didn't realize you're creating multiple arrays):

 

<?php
foreach( $Data as $index => $arraydata){
   foreach( $arraydata as $index2 => $arraydata2){
      foreach( $arraydata2 as $index3 => $arraydata3){
            if($index3!=$index2) {
               if ($Data[$index][$index2]["Name"] == $Data[$index][$index3]["Name"]){ //THIS IS PROBLEM LINE
                  echo "same name!";
         }
      }
   }
}
//...
?>

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.