vendredi 9 novembre 2018

PHP: array_filter for an object?

I have an array that I need to filter for certain things, for example, I might only want records that have the day of the week as Friday. As far as I'm aware this has never worked but it's taking an object and using array_filter on it. Can this work? Is there a better way or a way to do this on with object?

public function filterByDow($object)
{
    $current_dow=5;
    return array_values(array_filter($object, function ($array) use ($current_dow) {
        $array = (array) $array;
            if(!empty($array['day_id']) && $array['day_id'] > -1){
                if($array['day_id'] != $current_dow){
                    return false;
                }
            }
            return true;
    }));
}

$offers = $this->filterByDow($offers);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire