Stack Exchange
!in_array doesnt recognize category
I want to fill an array with categories and tried to prevent duplicates with in_array:
if ( $query->have_posts() ) {
    $categories = array();
    while ( $query->have_posts() ) {
        $query->the_post();
        foreach((get_the_category()) as $category) {
            if(!in_array($category, $categories)) {
                $categories[] = $category;
            }
        }
    };
} ?>

But the categories are still counted twice (or more, depending on the amount of posts).

Try it like this:
if ( $query->have_posts() ) {

    $categories = $category_ids = array();

    while ( $query->have_posts() ) {

        $query->the_post();

        foreach ( ( get_the_category() ) as $category ) {

            if ( ! in_array( $category->term_id, $category_ids ) ) {
                $category_ids[] = $category->term_id;
                $categories[] = $category;
            }

        }
    };
}

get_the_category() returns an array of objects. I didn't check the in_array documentation, but I suspect it doesn't include object matching. $category_ids is an array of just the ids used for comparison.
