Lately, dealing with databases, I've been finding myself
needing to know if one array, $a, is a proper subset of $b.
Mathematically, this is asking (in set theory) [excuse the
use of u and n instead of proper Unicode):
( A u B ) n ( ~ B )
What this does is it first limits to known values, then
looks for anything outside of B but in the union of A and B
(which would be those things in A which are not also in B).
If any value exists in this set, then A is NOT a proper
subset of B, because a value exists in A but not in B. For
A to be a proper subset, all values in A must be in B.
I'm sure this could easily be done any number of ways but
this seems to work for me. It's not got a lot of error
detection such as sterilizing inputs or checking input
types.
// bool array_subset( array, array )
// Returns true if $a is a proper subset of $b, returns
false otherwise.
function array_subset( $a, $b )
{
if( count( array_diff( array_merge($a,$b), $b)) == 0 )
return true;
else
return false;
}
----
Server IP: 209.41.74.194
Probable Submitter: 209.155.240.195
----
Manual Page -- http://www
.php.net/manual/en/ref.array.php
Edit -- https://master
.php.net/note/edit/78502
Del: integrated -- h
ttps://master.php.net/note/delete/78502/integrated
Del: useless -- http
s://master.php.net/note/delete/78502/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78502/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78502/spam
Del: non-english --
https://master.php.net/note/delete/78502/non-english
Del: in docs -- http
s://master.php.net/note/delete/78502/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78502
Reject -- https://mast
er.php.net/note/reject/78502
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|