A Function to do....

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.
LVL 5
mnb93Asked:
Who is Participating?
 
OneNineEightNineCommented:
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
 
WaielEidCommented:
what are the arguments and of what type ?!
0
 
mnb93Author Commented:
Sorry, args:
($array) (Type is Array)
0
 
ForsProject ManagerCommented:
You can use any of the Array function the PHP provides like:
array_diff, array_intersect, array_search, array_unique, in_array
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.