Check if two arrays values are equal in PHP

if ( ! function_exists( 'wpcb_are_arrays_equal' ) ) {
    /**
     * wpcb_are_arrays_equal.
     *
     * @see https://wpcodebook.com/snippets/check-if-two-arrays-values-are-equal-in-php/
     */
    function wpcb_are_arrays_equal( $array1, $array2 ) {
        if ( count( $array1 ) != count( $array2 ) ) {
            return false;
        }
        $diff = array_diff( $array1, $array2 );
        return empty( $diff );
    }
}

You can add this snippet to your (child) theme's functions.php file.

No comments yet.

Leave a Reply