0

I have an array whose items are of a certain object type, let it be my_object.

The class defining the my_objects has a function that I want to use to filter the array. How can I specify that function when calling array_filter?

 class my_class{
     private $exclude;
     public filter_function(){
        return !$this->exclude;
     }
  }

 $array=array($my_object1, $my_object2, ....);

 $filtered=array_filter($array,'filter_function');//obviously does not work

my_object1 , my_object2 , ... are all instances of my_class and I want that the

$my_object1->filter_function()

$my_object2->filter_function()

,.....

be called for filtering the array.

2 Answers 2

2

If you want to filter the array based on return value of each individual object's filter_function method call, you could simply:

array_filter($array, function($obj) { return $obj->filter_function(); });

No need to clutter the original class with static methods as in Mark's answer.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but I get a PHP Parse error: syntax error, unexpected T_FUNCTION in that line. maybe the version I am using does not support that kind of calling? I use php 5.2
Anonymous functions became available in 5.3. In earlier versions you'd need to define the entire inner function outside the call, and use its name in array_filter callback. Which wouldn't be very different from what Mark suggested (except for where the function is defined).
1

You need to indicate the object as well as the method in your callback, using an array syntax as shown in the php docs for callbacks

class my_class{
     private $exclude;
     public filter_function(){
        return !$this->exclude;
     }
  }

$array = array($my_object1, $my_object2, ....);

$classFilter = new my_class();
$filtered = array_filter($array,[$classFilter, 'filter_function']);

In this case, you need to create an instance first, then use that instantiated object as the first element in the callback, and the method name as the second element

EDIT

However, if you're trying to filter this collection of my_class objects based on whether individual objects have the exclude set, then you need to have a filter method for the collection:

class my_class{
     private $exclude;
     public filter_function(){
        return !$this->exclude;
     }
     public static filter_collection($value){
        return $value->filter_function();
     }
  }

$array = array($my_object1, $my_object2, ....);

$filtered = array_filter($array,['my_class', 'filter_collection']);

2 Comments

Thanks Mark, Then $filtered=array_filter($array,[$array[0], 'filter_function']); should work?
No, because all that does is return the flag for an individual element (instance), and will then apply that rule to all the elements in the collection; it's not the callback function for the full collection.... see my edit

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.