Array Comparison

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
neesley
Forum Commoner
Posts: 26
Joined: Tue Aug 31, 2010 3:22 am

Array Comparison

Post by neesley »

How does one compare two arrays?

For example--I have two arrays. I would like a simple true/false as to whether any values from array 1 appear in array 2. in_array will only search for a single value no? A loop would work I'm sure, but there has to be a more elegant way...

Thoughts? Thanks.
KeithK
Forum Newbie
Posts: 1
Joined: Tue Mar 29, 2011 11:10 pm

Re: Array Comparison

Post by KeithK »

You might be looking for array_diff().
neesley
Forum Commoner
Posts: 26
Joined: Tue Aug 31, 2010 3:22 am

Re: Array Comparison

Post by neesley »

I did have that in mind. What does array_diff() return if it finds nothing in common among arrays? Thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array Comparison

Post by AbraCadaver »

Code: Select all

if(array_intersect($array2, $array1)) {
If there are common values then it will return an array with the common values which will evaluate to true. If not it will return an empty array which evaluates to false.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply