Jump to content

[SOLVED] Returing the array[x] value


rhyspaterson

Recommended Posts

Hey guys,

 

Searching through an array to find a specific string, which is fine and working. The code i am using is

 


//$userToEdit is the user i want to edit
//$userToEditLength is the string length of the name of the user i want to edit (i have also added '3' to the length to compensate for the <n>)

foreach ($arrayText as $t) {
      if (substr($t, 0, $userToEditLength) == "<n>$userToEdit") {
  echo "String Matched!";
      }
  }

 

This all works fine. However, i would also like to output (now that i have found found the string exists) what array value the string resides in. I.e, does the $userToEdit string exist in $arrayText[2], or $arrayText[5] or $arrayText[4123].

 

Is there any way to do this in my substr if statement?

 

Cheers lads.

Link to comment
https://forums.phpfreaks.com/topic/53250-solved-returing-the-arrayx-value/
Share on other sites

Ok, figured it out. Just needed a variable below my foreach :D

 

Code looks like this now:

 

  $lineNumber = 0;
  foreach ($arrayText as $t) {
  $lineNumber = $lineNumber + 1;
      if (substr($t, 0, $userToEditLength) == "<n>$userToEdit") {
  echo "<br />String Matched!";
  echo $lineNumber;
	// the username is therefore the next string argument
      }
  }
  	exit();

 

^_^

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.