array_intersect not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mehnaz
Forum Newbie
Posts: 20
Joined: Mon Jun 02, 2008 7:49 pm

array_intersect not working

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: array_intersect not working

Post 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");
Post Reply