djp1988 Posted November 5, 2010 Share Posted November 5, 2010 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 More sharing options...
justsomeguy Posted November 5, 2010 Share Posted November 5, 2010 You can declare it as global in the anonymous function. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.