4

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;
    }));
}

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

Sample data might be like:

$object = (object) array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 4]);
9
  • can u provide a sample data, so that i can simulate it in fiddler? Commented Nov 9, 2018 at 15:52
  • Sure. I've edited the question. thank you. Commented Nov 9, 2018 at 15:58
  • 1
    What exactly does not work with the given code? Why do you need to cast the inner array into an object? Commented Nov 9, 2018 at 15:59
  • It's part of something bigger which is very badly written but I'm just trying to figure out if this part should work or if there's a better way to do this to an object. Commented Nov 9, 2018 at 16:05
  • 1
    laravel.com/docs/5.7/collections#method-filter :-) ? Commented Nov 9, 2018 at 16:16

3 Answers 3

4

As from the comments the array is a laravel collection I guess the answer is:

$filtered = $collection->filter(function ($value, $key) { 
    return $value['day_id'] == 5; 
});

https://laravel.com/docs/5.7/collections#method-filter

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

1 Comment

This is exactly what I was looking for. Thanks.
3

try this

    <?php
$items = array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 5]);
function filterByDow($items, $dow = 5){
    return array_filter($items, function($item) use ($dow) {
        if($item['day_id'] == $dow){
            return true;
        }
    });

}

$resultArr = filterByDow($items);
print_r($resultArr);
?>

2 Comments

An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
This is exactly what I was looking for. However, I have submitted an edit with very minor improvements.
1

Try to create a collection and implement methods for filtering in it.

class Offer
{
    private $dayId;

    public function getDayId()
    {
        return $this->dayId;
    }

    public function setDayId($dayId)
    {
        return $this->dayId = $dayId;
    }
}

class OfferCollection
{
    const FRIDAY = 5;

    static $dayIds = [
        self::FRIDAY => 'Friday'
    ];

    private $offers = [];

    public function addOffer(Offer $offer)
    {
        $this->offers[] = $offer;
    }

    public function getOffersByDay($dayId)
    {
        $offers = [];

        if (in_array($dayId, self::$dayIds)) {
            foreach ($this->offers as $offer) {
                if ($offer->getDayId == $dayId) $offers[] = $offer;
            }
        }

        return $offers;
    }
}

Comments

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.