24

I have an objects array like this:

Array
(
    [945] => member Object
        (
            [id] => 13317
            [name] => Test 999
            [last_name] => Test 999
        )

    [54] => member Object
        (
            [id] => 13316
            [name] => Manuel
            [last_name] => Maria parra
        )

    [654] => member Object
        (
            [id] => 13315
            [name] => Byron 
            [last_name] => Castillo
        )

    [656] => member Object
        (
            [id] => 13314
            [name] => Cesar
            [last_name] => Vasquez
        )
)

I need to remove one of these objects according to an attribute value.
For example, I want to remove from the array the object id 13316.

5 Answers 5

75

Here is the functional approach:

$neededObjects = array_filter(
    $objects,
    function ($e) use ($idToFilter) {
        return $e->id != $idToFilter;
    }
);
Sign up to request clarification or add additional context in comments.

3 Comments

dude the array is the first array_filter parameter, not the last! php.net/manual/en/function.array-filter.php
Perfect, worth noting that you can define the closure like function ($e) use ($id_to_find) { to be able to pass a var that contains the ID to search for. Maybe I'm the only one that didn't know this though ;) PHP Manual - Example 3
Awsome.. thanks :D I used it to compare if array elements arrayA present in array of objects. $arrayA=array(14,18,30,50); $neededObjects = array_filter( $objects, function ($e) { return in_array ($e->id,$arrayA); } );
4
function filter_by_key($array, $member, $value) {
   $filtered = array();
   foreach($array as $k => $v) {
      if($v->$member != $value)
         $filtered[$k] = $v;
   }
   return $filtered;
}

$array = ...
$array = filter_by_key($array, 'id', 13316);

3 Comments

If you redefine your function in terms of array_filter you'll earn my up vote :D Perhaps filter_by_key would be a better name.
@erisco, Regardless I changed it to filter_by_key as you recommended.
My intuition is that a member is a value, so filter_by_member would return all members that satisfy a predicate based on its value (just a regular filter). Another bit of convention is that a filter function usually only excludes things that do not satisfy a predicate. That is why I'd like to see the function rewritten using array_filter, and why not since array_filter is already predefined to do what you need?
4

Use array search function :

//return array index of searched item

$key = array_search($search_value, array_column($list, 'column_name'));

$list[key]; //return array item

Comments

3

Since there is already plenty solutions given, I suggest an alternative to using the array:

$storage = new SplObjectStorage;  // create an Object Collection
$storage->attach($memberObject);  // add an object to it
$storage->detach($memberObject);  // remove that object

You could make this into a custom MemberCollection class with Finder methods and other utility operations, e.g.

class MemberCollection implements IteratorAggregate
{
    protected $_storage;
    public function __construct()
    {
        $this->_storage = new SplObjectStorage;
    }
    public function getIterator()
    {
        return $this->_storage;
    }
    public function addMember(IMember $member)
    {
        $this->_storage->attach($member);
    }
    public function removeMember(IMember $member)
    {
        $this->_storage->detach($member);
    }
    public function removeBy($property, $value)
    {
        foreach ($this->_storage as $member) {
            if($member->$property === $value) {
                $this->_storage->detach($member);
            }
        }
    }        
}

Might be overkill for your scenario though.

Comments

0
   foreach ($array as $key=>$value)
      if ($value->id==13316) {
         unset($array[$key]);
         break;
      }

1 Comment

Thank you for your answer, I have edited my answer, my keys are not sorted, I mean, they are not 0, 1, 2, 3 ... please take a new look at my awnser, I changed the array keys, thk!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.