I am trying to search for an instance of a class in PHP but for the life of me I can't figure out how to do this, other than the obvious of recursing through each item in an array and checking the values. I was hoping someone can help if there is a way to do this. Here is an example
class Item {
public $item;
public $test;
public function __construct($item) {
$this->item = $item;
$this->test = sprintf("%d - Hello", $item);
}
}
$array = [];
for($i=0; $i < 100; $i++) {
$array[] = new Item($i);
}
$foundItem = array_search(75, array_column($array, "item"));
echo print_r($foundItem, true);
I want $foundItem
to be the entire object returned.
Thanks in advance.