SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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 Code:
    <?php
    session_start
    ();
        include 
    'file.php';
            
    $my_array = array($_SESSION['name']);
                
    $fufile_get_contents("UserLog.html");
                    
    $lines[] = explode(' '$fu);
        foreach(
    $lines as $key) {
            foreach(
    $my_array as $index) {
                if(
    $key === $index) {
                    echo 
    "$key";
                }
            }
        }
    ?>

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Couldn't you use array_intersect_key?

    PHP Code:
    $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); 

  3. #3
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I go about deleting it. If array1 key matches array2 key delete array2 key from array2 and without a whitespace?

    PHP Code:
    $keys_from_array1_in_array2 array_intersect_key($my_array$lines);
    unset(
    $lines['$keys_from_array1_in_array2']); 

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Couldn't you use array_intersect_key?

    PHP Code:
    $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); 
    Quote Originally Posted by Mr_Tobacco View Post
    How would I go about deleting it. If array1 key matches array2 key delete array2 key from array2 and without a whitespace?
    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.

  5. #5
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could you show me an example code?

  6. #6
    SitePoint Zealot bronze trophy xMog's Avatar
    Join Date
    Mar 2011
    Posts
    159
    Mentioned
    3 Post(s)
    Tagged
    2 Thread(s)
    How about:

    Code:
    $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"

  7. #7
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    PHP Code:
    <?php
    session_start
    ();
        
    $lines = array();        
            include 
    'index.php';
                
    $my_array = array($_SESSION['name']);
                    
    $fufile_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);
    ?>

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    $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"

  9. #9
    SitePoint Enthusiast bronze trophy \\.\'s Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    72
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    $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"
    Could you show me a code example

  11. #11
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by \\.\ View Post
    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.
    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.

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    You have
    PHP Code:
            $key = (string) $results
            unset(
    $lines[$key]); 

    You probably want:
    PHP Code:
            foreach ($key in $results)
                unset(
    $lines[$key]); 


  13. #13
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    [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.

    PHP Code:
    session_start();
        
    $lines = array();        
            include 
    'index.php';
                
    $my_array = array($_SESSION['name']);
                    
    $fufile_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);
    ?> 

  14. #14
    SitePoint Enthusiast bronze trophy \\.\'s Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    72
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Seems to me that this would be better dealt with through a database.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •