5

I have an array that looks like this:

[
    {
        "date":"2015-07-10",
        "count":"1"
    },{
        "date":"2015-07-11",
        "count":"3"
    }
]

Which is given by the following Eloquent query:

$posts = Post::select(array(
        DB::raw('DATE(`created_at`) as `date`'),
        DB::raw('COUNT(*) as `count`')
    ))
    ->where('created_at', '>', Carbon::today()->subWeek())
    ->groupBy('date')
    ->orderBy('date', 'DESC')
    ->get()
    ->toArray();

How can I check if a certain date is already in the array? I've tried the following, but it returns false (i.e. that the value isn't in the array, even though it is):

return in_array("2015-07-10", $posts);

1 Answer 1

7

You can use array_column in this case.

return in_array("2015-07-10", array_column($posts, 'date'));

DOCS

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

2 Comments

That did it, thanks! I'll mark it as the answer when I can (10 minutes).
Awesome answer!

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.