note 105250 added to function.in-array

From: Date: Fri, 05 Aug 2011 00:39:25 +0000
Subject: note 105250 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-182240@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/105250 Del: integrated -- https://master.php.net/note/delete/105250/integrated Del: useless -- https://master.php.net/note/delete/105250/useless Del: bad code -- https://master.php.net/note/delete/105250/bad+code Del: spam -- https://master.php.net/note/delete/105250/spam Del: non-english -- https://master.php.net/note/delete/105250/non-english Del: in docs -- https://master.php.net/note/delete/105250/in+docs Del: other reasons-- https://master.php.net/note/delete/105250 Reject -- https://master.php.net/note/reject/105250 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#182240) next »