A Function to do....

Posted on 2006-03-25
Last Modified: 2006-11-18
I  need a function that:

Checks to see if every value is the same. (excluding 0's( eg. 1,1,1,0 are the same.))-> Returns the "same value" (eg. 1)
Else outputs false

Question by:mnb93
    LVL 2

    Expert Comment

    what are the arguments and of what type ?!
    LVL 5

    Author Comment

    Sorry, args:
    ($array) (Type is Array)
    LVL 1

    Accepted Solution

    This should work:

    function allSame($arr)
       $s = 5972383346264832; //a random number that will not be in the array by chance
       foreach ($arr as $value)
          if ($value != 0)
             if ($s == 5972383346264832)
                $s = $value;
               if ($s != $value)
                  return FALSE;
       return $s;
    LVL 3

    Expert Comment

    You can use any of the Array function the PHP provides like:
    array_diff, array_intersect, array_search, array_unique, in_array

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now