note 105251 added to function.in-array
| From: | LeaHayes at osu1 dot php dot net | Date: | Fri, 05 Aug 2011 00:40:20 +0000 |
| Subject: | note 105251 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182241@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 69.147.83.197
Probable Submitter: 92.232.71.252
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/105251
Del: integrated -- https://master.php.net/note/delete/105251/integrated
Del: useless -- https://master.php.net/note/delete/105251/useless
Del: bad code -- https://master.php.net/note/delete/105251/bad+code
Del: spam -- https://master.php.net/note/delete/105251/spam
Del: non-english -- https://master.php.net/note/delete/105251/non-english
Del: in docs -- https://master.php.net/note/delete/105251/in+docs
Del: other reasons-- https://master.php.net/note/delete/105251
Reject -- https://master.php.net/note/reject/105251
Search -- https://master.php.net/manage/user-notes.php