note 105250 deleted from function.in-array by danbrown
| From: | danbrown@php.net | Date: | Fri, 05 Aug 2011 14:49:20 +0000 |
| Subject: | note 105250 deleted from function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182255@lists.php.net to get a copy of this message | ||
Note Submitter: Lea Hayes
----
Determine whether an object field matches needle.
Usage Example:
---------------
$arr = array( new stdClass(), new stdClass() );
$arr[0]->colour = 'red';
$arr[1]->colour = 'green';
$arr[1]->state = 'enabled';
if (in_array_field('red', 'colour', $arr))
echo 'Item exists with colour red.';
if (in_array_field('magenta', 'colour', $arr))
echo 'Item exists with colour magenta.';
if (in_array_field('enabled', 'state', $arr))
echo 'Item exists with enabled state.';
Output:
--------
Item exists with colour red.
Item exists with enabled state.
function in_array_field($needle, $needle_field, $haystack, $strict = false) {
if ($strict) {
foreach ($haystack as $item)
if (isset($item->$needle_field) && $item->$needle_field === $needle)
return true;
}
else {
foreach ($haystack as $item)
if (isset($item->$needle_field) && $item->$needle_field == $needle)
return true;
}
return false;
}