Q:

Check if value is the only thing in an array using php

I have an array that looks like this...
Array
(
    [0] => red
    [1] => red
    [2] => red
)

I am trying to check if red is the only thing in the array, I would want it to fail if the array looked like this...
Array
(
    [0] => red
    [1] => yellow
    [2] => red
)

A:

Using array_unique() you can just count the number of occurances returned. If its > 1 you have not got all red
<?php
$array = ['red','red','red'];

if ( count(array_unique($array)) == 1 && array_unique($array)[0] == 'red' ) {
    echo 'all red';
} else {
    echo 'error';
}

