Question about array element in foreach loops

Hi,

I am beating my head against the desk. Seems I am missing something. Perhaps I could get suggestions.

I am passing an array of lines read from a file, and an array of filter strings from post data, into a function.
If there are no filter elements I just pass the lines array back to the calling function.
If there are filter elements, then each filter element is compared against each line. Lines having a matched filter gets pushed into a holding array until processing is done.
My problem is with iterating over the filter array elements. Everything works fine if I put a comma in the filter input box, but without a comma, my first filter element appears empty. Users should not be forced to add a filter at all, nor put a comma after a filter string.
I’m using explode to get the posted string into an array.

// this is where I set the include filter array, 
// and I think this is the issue when no comma is sent.
// $fin is initialized as $fin=array(); then populated with the explode statement
if(isset($_POST["filterin"]))  {$fin =  explode(",",$_POST["filterin"]);}

function filterin($lines, $fin){
	// $lines - array of lines from a file
	// $fin - 'Include' filter array from post data
	$tmparray = array();   //Holding array

	// Check if array is defined, if not just use all lines
	// I know this exist, It is initialized before the function
	if(!$fin){
		return $lines;
	}
	
	//If it only has one element and that is empty,
	//We can just move all lines
	if(count($fin)===1 && empty($array[0])){ 
		return $lines;
	}

	//We have more than 1 empty filter element,
	//So we need to compare all lines to include filters
	foreach($lines as $line){
		foreach($fin as $include){
			//If filter is empty just skip to next filter
			if(!empty($include)) {
				$pos = strpos($line,$include);
                // More processing here
				// this works if there is at least 1 comma in posted filter data
				file_put_contents("test.txt","include filter was not empty ".$include.PHP_EOL, FILE_APPEND);  
			}
			//otherwise this fires
			file_put_contents("test.txt","include filter was empty. ".$include.PHP_EOL, FILE_APPEND);  
		}
		unset($include);
	}
	unset($line);

	//Return the result array after pushing the elements over
	return $tmparray;
}

Thank You for anything you can see!

This morning I worked it out, I think the tiredness last night caused me to overlook some array name mistakes. If anyone is interested, this is what worked for me:

	function filterin($lines, $fin){
	// $lines - array of lines from a file
	// $fin - 'Include' filter array from post data
	$tmparray = array();   //Holding array

	// Check if array is defined, if not just use all lines
	// I know this exist, It is initialized before the function
	if(!$fin){
		return $lines;
	}
	
	//If it only has one element and that is empty,
	//We can just move all lines
	if(count($fin)===1 && empty($fin[0])){ 
		return $lines;
	}

	//We have more than 1 empty filter element,
	//So we need to compare all lines
	foreach($lines as $line){
		foreach($fin as $include){
			//If filter is empty just skip to next filter
			if(!empty($include)) {
				if(strpos($line,$include)!==false){
					array_push($tmparray,$line);
					break;
				}
			}
		}
		unset($include);
	}
	unset($line);

	//Return the result array after pushing the elements over
	return $tmparray;
}

Thanks for any who may have thought about this. And of course please feel free to give your insight.

Sponsor our Newsletter | Privacy Policy | Terms of Service