Link to home
Start Free TrialLog in
Avatar of nzrubin
nzrubinFlag for New Zealand

asked on

check if all items are in array php

hi guys
i want to check if one array contain all the items as second one has
$array_1 = "1,4,2,5";
$array_2 = "4,5,2";
if ($array_2 == $array_1)
 {YES}
else
{NO}

thanks a lot!

[[ phpBB zone removed by PenguinMod, EE Moderator, per Q_23571807.html ]]
Avatar of compfixer101
compfixer101
Flag of United States of America image

first, this has nothing to do with phpbb that i can tell. (i'll get a mod to remove it)

second, for being a premium service member this is a low amount of points..(the higher the point value the quicker and more experts will come.)

third, your question doesn't explain what you want.. so please explain it more..
Avatar of nzrubin

ASKER

sorry about phpbb
(i thought easier the question less i put points.....)

ill try to explain more: (you right)
i have two arrays for example
$array_1 = "1,4,2,5";
$array_2 = "4,5,2";
i want to compare two arrays to check if they have the same amount of values and the same values (even if they not in a right order). (values are always integers)
for example if arrays looks like below
$array_1 = "1,4,2,5";
$array_2 = "4,5,2,1";

if ($array_1 == $array_2)
{ echo "Done";}


Try this:
<?php
    function isArrayInArray($searchFor, $searchIn) {
        foreach($searchFor as $key) {
            $count += in_array($key,$searchIn);
        }
 
        return $count==sizeof($searchFor);
    }
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of silveuk
silveuk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nzrubin

ASKER

it works with very minor adjustment

for ($i=0;$i<=count($array1);$i++)
{
   if(!$array1[$i]==$array2[$i])
   {
    echo  arrays dotn match;
     break;
   }
}
thanks