Jump to content

array_map on objects


djp1988

Recommended Posts

I have an array of objects, each object has an id property. I need to get an object from the array based on the id property.using array_map, I usually do the following:

$looking_for_object_id = 3;$ids = array_map(create_function('$a','return $a->id;'),$my_array);$key = array_search($looking_for_object_id, $ids);if($key !== false){$my_obj = $my_array[$key];}

How could I simplify this ? I would have prefered doing something like:

$my_obj = array_map(create_function('$a','if($the_id == $a->id) return $a;'),$my_array);

but obviously I get an error because $the_id is from outside the function

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...