?
Solved

A Function to do....

Posted on 2006-03-25
4
Medium Priority
?
248 Views
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

Thanks.
0
Comment
Question by:mnb93
4 Comments
 
LVL 2

Expert Comment

by:WaielEid
ID: 16292157
what are the arguments and of what type ?!
0
 
LVL 5

Author Comment

by:mnb93
ID: 16292399
Sorry, args:
($array) (Type is Array)
0
 
LVL 1

Accepted Solution

by:
OneNineEightNine earned 2000 total points
ID: 16294020
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;
        }
        else
        {
           if ($s != $value)
           {
              return FALSE;
           }
        }
      }
   }
   return $s;
}
0
 
LVL 3

Expert Comment

by:Fors
ID: 16304371
You can use any of the Array function the PHP provides like:
array_diff, array_intersect, array_search, array_unique, in_array
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
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 count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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