0

I have an array of Objects that have a member name.

How can I find the first object which fulfills the condition

Object->name == "foo"

In practice, is there a faster way than:

$needle = null;
foreach($myarray as $myobject)
{
    if($myobject->name == "foo")
    {
        $needle = $myobject;
        break;
    }
}
quimnuss
  • 1,503
  • 2
  • 17
  • 37
  • That's as fast as you can achieve, although if the name is available directly then calling an access method could be optimized out. – SPlatten Feb 07 '18 at 10:50
  • I'll edit it, thx for the comment. – quimnuss Feb 07 '18 at 10:56
  • @SPlatten Using `array_search()` should be more optimized because the loop is made internally and avoid a copy of object's reference caused by `foreach`. – Syscall Feb 07 '18 at 11:18

0 Answers0