note 123677 added to function.in-array
| From: | JoeF@php.net | Date: | Sun, 10 Mar 2019 15:48:36 +0000 |
| Subject: | note 123677 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210346@lists.php.net to get a copy of this message | ||
If you use in_array to check if an object is in an array, using non-strict will check if any
instance of the class is already in the array. Using strict will check if that specific instance of
the class is in the object.
For example:
<?php
class foo {
protected $obj_array = array();
public function add_object(foo $foo, bool $strict) {
if (in_array($foo, $this->obj_array, $strict)) {
echo 'Already present.<br/>';
}
else {
$this->obj_array[] = $foo;
echo 'Added to array.<br/>';
}
}
}
$foo = new foo;
$bar = new foo;
$foobar = new foo;
$foo->add_object($bar, FALSE); // Added to array.
$foo->add_object($bar, FALSE); // Already present.
$foo->add_object($foobar, FALSE); // Already present.
$foo->add_object($foobar, TRUE); // Added to array.
?>
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.34 (proxied: 38.27.142.170)
----
Manual Page -- http://php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/123677
Del: integrated -- https://master.php.net/note/delete/123677/integrated
Del: useless -- https://master.php.net/note/delete/123677/useless
Del: bad code -- https://master.php.net/note/delete/123677/bad+code
Del: spam -- https://master.php.net/note/delete/123677/spam
Del: non-english -- https://master.php.net/note/delete/123677/non-english
Del: in docs -- https://master.php.net/note/delete/123677/in+docs
Del: other reasons-- https://master.php.net/note/delete/123677
Reject -- https://master.php.net/note/reject/123677
Search -- https://master.php.net/manage/user-notes.php