Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7917155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:03:18+00:00 2026-06-03T15:03:18+00:00

I need to check if all values in an array equal the same thing.

  • 0

I need to check if all values in an array equal the same thing.

For example:

$allValues = array(
    'true',
    'true',
    'true',
);

If every value in the array equals 'true' then I want to echo 'all true'. If any value in the array equals 'false' then I want to echo 'some false'

Any idea on how I can do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:03:19+00:00Added an answer on June 3, 2026 at 3:03 pm

    All values equal the test value:

    // note, "count(array_flip($allvalues))" is a tricky but very fast way to count the unique values.
    // "end($allvalues)" is a way to get an arbitrary value from an array without needing to know a valid array key. For example, assuming $allvalues[0] exists may not be true.
    if (count(array_flip($allvalues)) === 1 && end($allvalues) === 'true') {
    
    
    }
    

    or just test for the existence of the thing you don’t want:

    if (in_array('false', $allvalues, true)) {
    
    }
    

    Prefer the latter method if you’re sure that there’s only 2 possible values that could be in the array, as it’s much more efficient. But if in doubt, a slow program is better than an incorrect program, so use the first method.

    If you can’t use the second method, your array is very large, and the contents of the array is likely to have more than 1 value (especially if the 2nd value is likely to occur near the beginning of the array), it may be much faster to do the following:

    /**
     * Checks if an array contains at most 1 distinct value.
     * Optionally, restrict what the 1 distinct value is permitted to be via
     * a user supplied testValue.
     *
     * @param array $arr - Array to check
     * @param null $testValue - Optional value to restrict which distinct value the array is permitted to contain.
     * @return bool - false if the array contains more than 1 distinct value, or contains a value other than your supplied testValue.
     * @assert isHomogenous([]) === true
     * @assert isHomogenous([], 2) === true
     * @assert isHomogenous([2]) === true
     * @assert isHomogenous([2, 3]) === false
     * @assert isHomogenous([2, 2]) === true
     * @assert isHomogenous([2, 2], 2) === true
     * @assert isHomogenous([2, 2], 3) === false
     * @assert isHomogenous([2, 3], 3) === false
     * @assert isHomogenous([null, null], null) === true
     */
    function isHomogenous(array $arr, $testValue = null) {
        // If they did not pass the 2nd func argument, then we will use an arbitrary value in the $arr (that happens to be the first value).
        // By using func_num_args() to test for this, we can properly support testing for an array filled with nulls, if desired.
        // ie isHomogenous([null, null], null) === true
        $testValue = func_num_args() > 1 ? $testValue : reset($arr);
        foreach ($arr as $val) {
            if ($testValue !== $val) {
                return false;
            }
        }
        return true;
    }
    

    Note: Some answers interpret the original question as (1) how to check if all values are the same, while others interpreted it as (2) how to check if all values are the same and make sure that value equals the test value. The solution you choose should be mindful of that detail.

    My first 2 solutions answered #2. My isHomogenous() function answers #1, or #2 if you pass it the 2nd arg.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of dictionaries. I need to check if all the dictionaries
I need to check the type of each element in an array... for(id obj
I need to get the values of all checkboxes that are currently checked using
Hi All I have a group of check box having same name so as
I need to check whether document element value is changed after particular operation using
I need help with the Check button. After a user adds all the 42
First, i have an array that has all the option value must be selected
I have 2 arrays, first array holds all the values for resource_wastage and 2nd
I need to know if all the client-side validator checks have succeeded, or if
I need to check in some fast way if there is any text nodes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.