php - Check for same Array values -


i want check in single statement, if contents of array same or not. challenge me, not able check uniqueness of values inside array.

also want make sure values same as, "s". example array:

$myarray = array("s", "s", "s");  // true $myarray = array("s", "s", "s");  // false 

is possible using single statement? in advance.

yes, possible array_count_values. looking for?

the function array_count_values() count number of unique values in array. if count of 1, unique said.

count(array_count_values($myarray)) == 1 

also, can check 1 of array values whatever value wanted check.

$myarray[0] == "s" 

so combining these 2 in single condition:

count(array_count_values($myarray)) == 1 && $myarray[0] == "s" 

this return true or false.

full working code

<?php     // let's have 2 arrays.     $myarray1 = array("s", "s", "s");  // should return true     $myarray2 = array("s", "s", "s");  // should return false      // our function     function checkarray($thearray, $value) {       return count(array_count_values($myarray)) == 1 && $myarray[0] == $value;     }      // checks     checkarray($myarray1, "s"); // true     checkarray($myarray2, "s"); // false 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -