note 78502 added to ref.array
| From: | cyberchrist at futura dot net | Date: | Mon, 15 Oct 2007 13:33:16 +0000 |
| Subject: | note 78502 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131399@lists.php.net to get a copy of this message | ||
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 -- https://master.php.net/note/delete/78502/integrated
Del: useless -- https://master.php.net/note/delete/78502/useless
Del: bad code -- https://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 -- https://master.php.net/note/delete/78502/in+docs
Del: other reasons-- https://master.php.net/note/delete/78502
Reject -- https://master.php.net/note/reject/78502
Search -- https://master.php.net/manage/user-notes.php