Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,783 answers

573 users

How to check whether an array is subset of another array in PHP

1 Answer

0 votes
function isSubset($arr1, $arr2) {
    $size1 = count($arr1);
    $size2 = count($arr2);

    for ($i = 0; $i < $size2; $i++) {
        for ($j = 0; $j < $size1; $j++) {
            if ($arr2[$i] == $arr1[$j])
                break;
            }
            if ($j == $size1)
                return false;
        }
        return true;
    }


$arr1 = array( 5, 1, 8, 12, 40, 7, 9, 100 );
$arr2 = array( 8, 40, 9, 1 );
 
if (isSubset($arr1, $arr2))
    echo "yes";
else
    echo "no";


 
 
 
   
/*
run:
   
yes
   
*/

 





answered Dec 21, 2021 by avibootz

Related questions

...