Jump to content

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


physaux

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

Archived

This topic is now archived and is closed to further replies.

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