Jump to content

Array Weirdness


STaRDoGG

Recommended Posts

Hi all,

 

Running into a strange issue with an array. Anyone able to spot the problem?

 

To sum up, if i use the manual array creation, the foreach loop works, but when I use the explode method to create the array, it doesn't work. It will return "no match", even if it is a match. I have added "##" to the comments in the code below on where the problems are arising.

 

Thanks for any help in advance.

 

function Member_Exclusions ($GSName) {

	// To ensure we don't have to worry about case sensitivity, lets do all comparisons as lowercase
	$GSName = strtolower($GSName);

	// ## Uncommenting the 2 lines below, makes the foreach loop work correctly ...
	//$GSExclude[0] = "Lisa";
	//$GSExclude[1] = "Michelle";

                // ## However, creating the array with the following line, does not work ...
                // (The array is created from a text box, separating names by a new line)
                // Using the echo's as a test, the array IS being created correctly ...
	$GSExclude = explode("\n", variable_get('Member_filter_exclusions_list', ''));
	//echo $GSExclude[0]; //test
	//echo $GSExclude[1];

	// Loop through the array of members, looking for the current one
	foreach ($GSExclude as $value) {

		$value = strtolower($value);
		//echo $value;
		//echo $value[0];
                        // ## Here is where the issue is: $value is echoing both names form the array
                        // on a single line, rather than just one name per index in the array.

		if ($value == $GSName) {
			// Found our friend
			$GSResult = "match";
			break;
		}else{
			// Nope
			$GSResult = "no match";
		}
	}
	return $GSResult;
}

 

Link to comment
https://forums.phpfreaks.com/topic/167843-array-weirdness/
Share on other sites

Instead of doing a plain "echo" use

<?php
echo '<pre>' . print_r($GSExclude,true) . '</pre>';
?>

to dump the array. You probably have some unseen whitespace in each entry after the explode, so trim each entry:

<?php
$GSExclude = array_map('trim',explode("\n", variable_get('Member_filter_exclusions_list', '')));
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/167843-array-weirdness/#findComment-885249
Share on other sites

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.