Re: Performance question about create_function

From: Date: Sat, 24 Oct 2009 13:09:51 +0000
Subject: Re: Performance question about create_function
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-45876@lists.php.net to get a copy of this message
On Mon, Oct 19, 2009 at 1:13 PM, Mathieu Suen <mathieu.suen@easyflirt.com> wrote: > Looking at the way array_map is working, it could worth to use > create_function for object. > > For example if I got a list of object: > > $callback = create_function($element, 'return $selement->getId();'); > $ids = array_map($callback, $someInstances); > > > But I don't know if it's cost a lot in performance instead of the ugly > verbose way: > > $ids = array(); > foreach($someInstances) > { >        $ids[] = $someInstances->getId(); > } I think what you want to do can be done with anonymous functions: $arr1 = array( (object) array("id" => 56), (object) array("id" => 42) ); $ids = array_map(function($obj) { return $obj->id; }, $arr1); print_r($ids); produces: Array ( [0] => 56 [1] => 42 ) Michael -- http://beebo.org +44 78 2118 9049

« previous php.internals (#45876) next »