Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array_filter on objects

I'm trying to use array_filter on an array of objects, and using a public method of the foo-class as a callback. I don't know how to do this though.

I was getting this result: Fatal error: Using $this when not in object context which I can guess is because it's calling the bar method in a static manner, but how to pass the objects to the array_filter callback method properly?

function foobar_filter($obj) {
    return $obj->bar();
}

class foo {
    private $value;
    public function __construct($value) {
        $this->value = $value;
    }
    public function bar() {
        // checking if $this is set to avoid "using this when not in object yadayada"-message
        if ($this) return ($this->value > 10);
        else return false;
    }
}

$arr = array(new foo(12), new foo(42), new foo(4));
var_dump($arr);

// Here is the workaround that makes it work, but I'd like to use the objects' method directly. This is the result that I am expecting to get from $arr3 as well
$arr2 = array_filter($arr, "foobar_filter");
var_dump($arr2);

// I would like this to work, somehow...
$arr3 = array_filter($arr, array(foo, "bar"));
var_dump($arr3);

So the result I expect is an array with two objects of the class foo with the values 12 and 42.

For your information, I am using PHP 5.2.6 but I would be happy if it's possible with any PHP-version.

like image 506
Simon Forsberg Avatar asked Jun 13 '12 14:06

Simon Forsberg


People also ask

How to filter data from array of objects in PHP?

The array_filter() function filters the values of an array using a callback function. This function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

How do you sort an array of objects in PHP?

Approach: The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.

Is PHP an array?

Definition and UsageThe is_array() function checks whether a variable is an array or not. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.


2 Answers

you can use Closure (>= PHP 5.3) in array_filter method like this

$arrX = array_filter($arr, function($element) {
    return $element->bar();
});
var_dump($arrX)
like image 119
silly Avatar answered Oct 11 '22 03:10

silly


I think you can call it statically like this:

class foo {
    private $value;
    public function __construct($value) {
       $this->value = $value;
    }
    public static function bar($a) {        
        if ($a) return ($a->value > 10);
        else return false;
    }
}

$arr = array(new foo(12), new foo(42), new foo(4));

$arr3 = array_filter($arr, array('foo', "bar"));
var_dump($arr3);
like image 21
davidgrcr Avatar answered Oct 11 '22 04:10

davidgrcr