Compare values in a single array and return the key in php -


i have array this:

$age=array("peter"=>43,"ben"=>67); .

the array contains 2 key value pairs. first need check if values of these 2 keys same. if same returns key of these 2 value, otherwise return false. here value 43 , 67 not same should return false. if 2 values same :

$age=array("peter"=>43,"ben"=>43); .

it should return key "peter" , key "ben" , maybe store keys in array reason find if 2 people of same age if same age several other things. appreciate help.

just unique values , see if there 1:

if(count(array_unique($age)) === 1) {      return array_keys($age); } else {      return false; } 

because bored, here 2 others.

assuming 2 elements:

if(($v = array_values($age)) && $v[0] === $v[1]) {      return array_keys($age); } else {      return false; } 

also, should work multiples:

if((array_sum($age) % count($age)) === 0) {      return array_keys($age); } else {      return false; } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -