I need a function that compares the elements of an array and returns true if all elements are = and false if not.
I am not getting very far trying to write it. does one already exist?
Comparing array elements
Moderator: General Moderators
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
uhh.. maybe you could use ==??
Code: Select all
<?
$arr1 = array('1','2','3');
$arr2 = array('1','2','2'); //change the second 2 to a 3 and it will echo equal!
if ($arr1 == $arr2){
echo "equal!";
}else{
echo "not equal!";
}
?>I think he means, if you have an array, like array('one', 'one', 'one') how to show they are all the same (which they are in this case) or show they are not all the same, array('one', 'one', 'two'). In which case you could do something like
..but i may have got the wrong end of the stick 
Code: Select all
$array = array('one', 'one', 'one');
$store = array();
foreach($array as $val){
if(!in_array($val, $store)){
$store[] = $val;
}
}
echo (count($store) == 1) ? 'The same' : 'Different';