Page 1 of 1

array_intersect not working

Posted: Tue Jun 16, 2009 11:18 pm
by Mehnaz
I have the following code

Code: Select all

<?php
 
$stop_words[] = array("a", "about", "above", "across", "after", "afterwards");
 
$custom_words[] = array("abouts", "a", "to", "from", "across");
print_r(array_intersect($custom_words,$stop_words));
 
?>
but the array_intersect does not print the common words. It prints all words in the first array in arguments list. Here the out I get is
Array ( [0] => Array ( [0] => abouts [1] => a [2] => to [3] => from [4] => across ) )

I want to use array_intesect for bigger arrays.

Please help

Mehnaz

Re: array_intersect not working

Posted: Tue Jun 16, 2009 11:43 pm
by requinix
$stop_words and $custom_words are arrays of arrays. array_intersect compares string representations, and since that's always "Array" then you get a copy of $custom_words.

Code: Select all

$stop_words = array("a", "about", "above", "across", "after", "afterwards");
$custom_words = array("abouts", "a", "to", "from", "across");