Jump to content

Check if array is subset of another


ecopetition

Recommended Posts

Hi, I've checked the PHP manual and Googled this but can't find anything that assists me.

 

Does anybody know if there exists a function issubset($childarray, $parentarray) that returns true if $childarray is a subset of $parentarray?  That is, if all the elements in $childarray are included in $parentarray.

 

Many thanks

Link to comment
Share on other sites

Thanks for your reply thorpe, but this function returns the difference in the arrays, how would one use that to see if it's a subarray of the parent array?

 

I read the notes for the function and didn't get anything similar to what I want, also Googled and it seems to have very few results for "subarray".

 

Thanks again.

Link to comment
Share on other sites

Or an array_diff and a few count statements -

 

<?php

$main = range(10,20);

$subarray = array(1,12,13);

$diff = array_diff($main,$subarray);

if(count($diff) == count($main) - count($subarray)){
echo "all elements in the subarray were found in the main array";
} else {
echo "not all the elements in the subarray were found in the main array";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.