Jump to content
Sign in to follow this  
djp1988

array_map on objects

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...