Check if value exists in associative JSON array

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 query request:

$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 specific date is already in an array? I've tried the following, but it returns false

(i.e. that the value is not in the array, even if it is):

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

      

+3


source to share


1 answer


In this case, you can use array_column

.

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

      



DOCS

+4


source







All Articles