ecopetition Posted February 6, 2012 Share Posted February 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256503-check-if-array-is-subset-of-another/ Share on other sites More sharing options...
trq Posted February 6, 2012 Share Posted February 6, 2012 array_diff will provide you with a solution. Quote Link to comment https://forums.phpfreaks.com/topic/256503-check-if-array-is-subset-of-another/#findComment-1314938 Share on other sites More sharing options...
ecopetition Posted February 6, 2012 Author Share Posted February 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256503-check-if-array-is-subset-of-another/#findComment-1315188 Share on other sites More sharing options...
litebearer Posted February 6, 2012 Share Posted February 6, 2012 Perhaps write your own??? <?PHP function issubset($childarray, $parentarray); $c = count($childarray); $valid = 1; for($i=0;$i<$c;$i++) { if(!in_array($childarray[$i], $parentarray)) { $valid = 0; return $valid; } } return $valid; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256503-check-if-array-is-subset-of-another/#findComment-1315193 Share on other sites More sharing options...
PFMaBiSmAd Posted February 6, 2012 Share Posted February 6, 2012 You can probably use array_intersect , followed by an array_diff to find if all the elements in one array are found in another array. Quote Link to comment https://forums.phpfreaks.com/topic/256503-check-if-array-is-subset-of-another/#findComment-1315195 Share on other sites More sharing options...
PFMaBiSmAd Posted February 6, 2012 Share Posted February 6, 2012 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/256503-check-if-array-is-subset-of-another/#findComment-1315201 Share on other sites More sharing options...
kicken Posted February 6, 2012 Share Posted February 6, 2012 A quick array_intersect and count should work. <?php $a1 = array(2,4,6,; $a2 = array(1,2,3,4,5,6,7,8,9,10); function issubset($arr1, $arr2){ return count($a1)==count(array_intersect($a1, $a2)); } var_dump(issubset($a1, $a2)); Quote Link to comment https://forums.phpfreaks.com/topic/256503-check-if-array-is-subset-of-another/#findComment-1315227 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.