Jump to content


Photo

array


  • Please log in to reply
1 reply to this topic

#1 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 20 August 2006 - 04:06 PM

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"
<?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";
} 
?> 
but none of them seem to work

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 04:11 PM

Use array_intersect (http://www.php.net/array_intersect) or array_diff (http://www.php.net/array_diff).

$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";
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users