note 98455 added to function.in-array
| From: | WillianGustavoVeiga at osu1 dot php dot net | Date: | Wed, 16 Jun 2010 14:41:42 +0000 |
| Subject: | note 98455 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170183@lists.php.net to get a copy of this message | ||
I wrote a function that search for a property value of an object in an array of objects:
<?php
function property_value_in_array($array, $property, $value) {
$flag = false;
foreach($array as $object) {
if(!is_object($object) || !property_exists($object, $property)) {
return false;
}
if($object->$property == $value) {
$flag = true;
}
}
return $flag;
}
$obj1 = new StdClass();
$obj2 = new StdClass();
$obj3 = new StdClass();
$obj1->name = 'Object 1';
$obj1->age = 12;
$obj2->name = 'Object 2';
$obj2->age = 13;
$obj3->name = 'Object 3';
$obj3->age = 14;
/* It returns true because there is the "14" value in the "age" property of an
object within the array. */
echo property_value_in_array(array($obj1, $obj2, $obj3), 'age', 14);
?>
----
Server IP: 187.16.23.128
Probable Submitter: 189.26.144.96
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/98455
Del: integrated -- https://master.php.net/note/delete/98455/integrated
Del: useless -- https://master.php.net/note/delete/98455/useless
Del: bad code -- https://master.php.net/note/delete/98455/bad+code
Del: spam -- https://master.php.net/note/delete/98455/spam
Del: non-english -- https://master.php.net/note/delete/98455/non-english
Del: in docs -- https://master.php.net/note/delete/98455/in+docs
Del: other reasons-- https://master.php.net/note/delete/98455
Reject -- https://master.php.net/note/reject/98455
Search -- https://master.php.net/manage/user-notes.php