note 123677 deleted from function.in-array by cmb
| From: | cmb@php.net | Date: | Mon, 11 Mar 2019 11:46:23 +0000 |
| Subject: | note 123677 deleted from function.in-array by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210349@lists.php.net to get a copy of this message | ||
Note Submitter: Joe F
----
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.
?>