Wednesday, October 9, 2013

Filter an array of objects using object's property

Iterate through the array and return true or false based on the condition

$new_array = array_filter($array, function($obj){
    if (isset($obj->admins)) {
        foreach ($obj->admins as $admin) {
            if ($admin->member == 11) return false;
        }
    }
    return true;
});

No comments:

Post a Comment