I'm Stuck on comparing keys in two arrays

I’m stuck I think it has something to do with the session name. I want it so it compares the log list in $key and if found a match with $index it echos out the $key it found. Basically doing this as trial so later I can work with using unset to remove that key from the list. here is the code

<?php
session_start();
	include 'file.php';
		$my_array = array($_SESSION['name']);
			$fu= file_get_contents("UserLog.html");
				$lines[] = explode(' ', $fu);
	foreach($lines as $key) {
		foreach($my_array as $index) {
			if($key === $index) {
				echo "$key";
			}
		}
	}
?>

Couldn’t you use array_intersect_key?

$array1 = array('key1' => "value1", 'key2' => "value2");
$array2 = array('key2' => "value3", 'key3' => "value4");

$keys_from_array1_in_array2 = array_intersect_key($array1, $array2);
var_dump($keys_from_array1_in_array2);

How would I go about deleting it. If array1 key matches array2 key delete array2 key from array2 and without a whitespace?


$keys_from_array1_in_array2 = array_intersect_key($my_array, $lines);
unset($lines['$keys_from_array1_in_array2']);  

You’d loop through the keys that are found in both and unset it from array2. $keys_from_array1_in_array2 is an array of keys found in both arrays. Loop through them and unset that item.

could you show me an example code?

How about:

$array1 = array('key1' => "value1", 'key2' => "value2", 'key3' => 'value3');
$array2 = array('key2' => "value3", 'key3' => "value4", 'key4' => 'value5', 'key5' => 'value6');

$keys_from_array1_in_array2 = array_intersect_key($array1, $array2);

foreach ($keys_from_array1_in_array2 as $key => $value) {
	// Put line here to delete an item from an array with the key
}

var_dump($array2);

To find the “foreach” line, you could have googled something like: “how to loop an array in php”
Hint for the missing line, Google is again the answer. Try something like “PHP How to delete an array element based on key” :slight_smile:

Thanks for the advice though I wrote my own work around. The script is able to find the SESSION name and the foreach loop is working but the problem is getting the unset() to work by completely removing the SESSION’s name in the $lines array. As you can see it’s still there from the print_r(). I think the problem is the unset($lines[$key]); The unset was giving me errors on the page so I added $key = (string) $results; Any help appreciated of course :confused:


<?php
session_start();
	$lines = array();		
		include 'index.php';
			$my_array = array($_SESSION['name']);
				$fu= file_get_contents("UserLog.html");
					$string_array = implode(',', $my_array);
						echo "SESSION word is: $string_array <br><br>";
					$lines = explode(' ', $fu);
	foreach ($lines as $value) {
		if (strpos($value, $string_array) !== false) {
			$results[] = $value;
		}
	}
		if( empty($results) ) {
			echo 'No matches found.';
		}
	else {
		$key = (string) $results;
		unset($lines[$key]); }
	echo "New line array keys: <br>";
print_r($lines);
?>

$results is an array but you are treating it as a string in your else… You need to loop over it and unset it one by one. Using (string) on an array will likely return “Array”

it is often better to explain your objective or need for checking a session variables value against the content of a page, then someone may be able to provide a more suitable answer to your need.

Could you show me a code example

So I can compare it to the $lines array from userlog.html. Delete the value in the $lines array that is the same as the word in the Session then open the userlog.html and write in the userlog.html the new $lines array. By doing this it would clear that session name off of the userlog.html list.

You have

        $key = (string) $results; 
        unset($lines[$key]); 
}

You probably want:

        foreach ($key in $results)
            unset($lines[$key]); 
}

[SOLVED]

Here is my final written code and finally working version of how to extract a Session name, match it with a list array then delete the match session in that array. Once finished the new array is written in the previous extracted array file. The code includes error checking. :injured:


session_start();
	$lines = array();		
		include 'index.php';
			$my_array = array($_SESSION['name']);
				$fu= file_get_contents("UserLog.html");
	$string_array = implode(',', $my_array);
		echo "SESSION word is: $string_array <br><br>";
	$lines = explode(' ', $fu);
			for($i = 0; $i < 5; $i++) {
				if(strpos($lines[$i], $string_array) !== false) {
					echo "<br>found Session match <br>";
					echo "Unseting match.. <br><br>";
					unset($lines[$i]);
				}
				else {
					echo "no Session found <br>";
				}
			}
	echo "<br>New line array keys/values: <br><br>";
print_r($lines);

$newlines = implode(' ', $lines);

$fp = fopen("UserLog.html", "w");
fwrite( $fp, $newlines);
fclose($fp);
?>


Seems to me that this would be better dealt with through a database.