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.

Simon Arnold
  • 15,849
  • 7
  • 67
  • 85
el_quick
  • 4,656
  • 11
  • 45
  • 53

5 Answers5

76

Here is the functional approach:

$neededObjects = array_filter(
    $objects,
    function ($e) use ($idToFilter) {
        return $e->id != $idToFilter;
    }
);
helle
  • 11,183
  • 9
  • 56
  • 83
erisco
  • 14,154
  • 2
  • 40
  • 45
  • 8
    dude the array is the first array_filter parameter, not the last! http://php.net/manual/en/function.array-filter.php – Pierlo Upitup Dec 03 '12 at 09:15
  • 9
    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](http://php.net/manual/en/functions.anonymous.php) – Ollie Mar 29 '13 at 09:03
  • 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); } ); – Parag Jan 31 '15 at 06:39
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
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
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);
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 1
    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 Nov 12 '10 at 16:08
  • @erisco, Regardless I changed it to `filter_by_key` as you recommended. – Jacob Relkin Nov 12 '10 at 16:10
  • 2
    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? – erisco Nov 12 '10 at 16:16
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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
0
   foreach ($array as $key=>$value)
      if ($value->id==13316) {
         unset($array[$key]);
         break;
      }
bcosca
  • 17,371
  • 5
  • 40
  • 51
  • 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! – el_quick Nov 12 '10 at 15:45