Jump to content

Archived

This topic is now archived and is closed to further replies.

desithugg

array

Recommended Posts

umm i want to check if all the values in $array2 exist in $array1.And ouput something depending on if they exist or no.
ive tried "in_array","array_search","array_key_exists"
[code]
<?php
$a1 = array(1, 2, 3, 4, 5, 6);
$a2 = array(1, 2, 3, 4, 5);
if(in_array($a2, $a1))
{
echo"right";
}
else
{
echo"wrong";
}
?>
[/code]
but none of them seem to work

Share this post


Link to post
Share on other sites
Use array_intersect (http://www.php.net/array_intersect) or array_diff (http://www.php.net/array_diff).

[code]$a1 = array(1, 2, 3, 4, 5, 6);
$a2 = array(1, 2, 3, 4, 5);
if(count(array_intersect($a2, $a1)) == count($a2)) {
echo"right";
} else {
echo"wrong";
}



if (count(array_diff($a2, $a1)) == 0) {
echo"right";
} else {
echo"wrong";
} [/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.