Hi there,
I'm trying to remove a value from an array. I seem to think unset is the right tool.. so anyhow, the following is what I'm using:

	function kda_stopwords() {
		$fopen = fopen("modules/shops/kdawords.txt", "r");
		$fsize = filesize("modules/shops/kdawords.txt");
		$fread = fread($fopen, $fsize);

	$words = explode("\n", $fread);

	foreach($words as $key) {
		if(in_array($key, $this->termnames)) {
			unset($this->termnames[$key]);
		}
	}
}

Everything works fine... and it finds the stopwords in the array (I've checked that) - anyhow... when it comes to the unset command I've used... it doesn't work. The value remains there?

Any ideas.

Thanks,

Chris Evans

    in_array() checks if the given value is in the array, not if a given key is in the array.
    You might be looking for [man]array_key_exists/man instead, or something else entirely.

      The actual in_array bit works. But you have pointed out that I need to know the key to remove it... is there anyway I can get the key from the array value?

      is_array works fine, but I suppose in order to use unset, I then need the key?

      Thanks,

      Chris

        Use array_search() instead of in_array().

        function kda_stopwords() {
        $fopen = fopen("modules/shops/kdawords.txt", "r");
        $fsize = filesize("modules/shops/kdawords.txt");
        $fread = fread($fopen, $fsize);
        
        $words = explode("\n", $fread);
        
        foreach($words as $value) {
        	if (($key = array_search($value, $this->termnames)) !== false) {
        		unset($this->termnames[$key];
        	}
        }
        }

          Hi,
          That's brilliant, thanks! 🙂 One thing it doesn't seem to want to do is remove the word 'a'

          I've got 'a' in the text, and it's also listed in kdawords.txt as a stopword but it's not removing it? :S

          I've checked as well, and it is being returned as found within the array and it's also got the correct key/value... yet it's not removing it?

          Chris

            Just noticed, it is removing a, but for some reason, not all of them:

            name for everybody works like a website shopping at name is just brilliant and your sure to find a good ebay bargain

            I might add, that's a test sentence lol 😃

            Also, when the words are split up individually, it shows them like this:

            Array
            (
                [0] => name
                [1] => for
                [2] => everybody
                [3] => works
                [4] => like
                [5] => a
                [6] => website
                [7] => shopping
                [8] => at
                [9] => name
                [10] => is
                [11] => just
                [12] => brilliant
                [13] => and
                [14] => your
                [15] => sure
                [16] => to
                [17] => find
                [18] => a
                [19] => good
                [20] => ebay
                [21] => bargain
            )
            

            Also, I'll say that the 'a' that it isn't removing is number 18.

            Thanks in advance,

            Chris Evans

              Write a Reply...