Page 1 of 1

Comparing array elements

Posted: Wed Mar 10, 2004 11:05 pm
by swendingo
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?

Posted: Wed Mar 10, 2004 11:11 pm
by Illusionist
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!";
}
?>

Posted: Wed Mar 10, 2004 11:24 pm
by markl999
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

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';
..but i may have got the wrong end of the stick ;)

Posted: Thu Mar 11, 2004 12:09 pm
by swendingo
exactly what I was looking for.


thank you mark