note 88596 added to function.array-intersect
| From: | karl at libsyn dot com | Date: | Fri, 30 Jan 2009 17:58:50 +0000 |
| Subject: | note 88596 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149992@lists.php.net to get a copy of this message | ||
Given a multidimensional array that represents AND/OR relationships (example below), you can use a
recursive function with array_intersect() to see if another array matches that set of relationships.
For example: array( array( 'red' ), array( 'white', 'blue' ) )
represents "red OR ( white AND blue )". array( 'red', array( 'white',
'blue' ) ) would work, too, BTW.
If I have array( 'red' ) and I want to see if it matches the AND/OR array, I use the
following function. It returns the matched array, but can just return a boolean if that's all
you need:
$needle = array( array( 'red' ), array( 'white', 'blue' ) );
$haystack = array( 'red' );
function findMatchingArray( $needle, $haystack ) {
foreach( $needle as $element ) {
$test_element = (array) $element;
if( count( $test_element ) == count( array_intersect( $test_element, $haystack ) ) ) {
return $element;
}
}
return false;
}
Pretty tough to describe what I needed it to do, but it worked. I don't know if anyone else
out there needs something like this, but hope this helps.
----
Server IP: 64.71.164.2
Probable Submitter: 209.114.137.18
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect.php
Edit -- https://master.php.net/note/edit/88596
Del: integrated -- https://master.php.net/note/delete/88596/integrated
Del: useless -- https://master.php.net/note/delete/88596/useless
Del: bad code -- https://master.php.net/note/delete/88596/bad+code
Del: spam -- https://master.php.net/note/delete/88596/spam
Del: non-english -- https://master.php.net/note/delete/88596/non-english
Del: in docs -- https://master.php.net/note/delete/88596/in+docs
Del: other reasons-- https://master.php.net/note/delete/88596
Reject -- https://master.php.net/note/reject/88596
Search -- https://master.php.net/manage/user-notes.php