Re: String compare of 7 text strings

From: Date: Sat, 14 Aug 2004 15:12:16 +0000
Subject: Re: String compare of 7 text strings
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-193801@lists.php.net to get a copy of this message
if ( count ( array_unique ( $array ) ) == 1 ) { // all fields have the same value } we could post solution to this problem for ever... - Hannes On Sat, 14 Aug 2004 15:24:03 +0100 M.Ford@leedsmet.ac.uk (Mike Ford) wrote: > -----Original Message----- > From: Brent Clements > To: php-general@lists.php.net > > I wanted to see if anyone has an easier way to do this. The end result > is this: I need to compare 7 different text strings(which are in an > array). They should all be the same, if they are not the same, a message > should be outputted saying they weren't. > > How would one do this outside of using a huge if/then statement? > > --------------------------- > > Two approaches come to mind off the top of my head: > > $values = array_count_values($array); > > if (count($values)>1): > // there's more than one unique value in the array > endif; > > or... > > $value = $array[0]; > for ($i=1; $i<7; ++$i): > if ($array[$i]!= $value): > // this value doesn't match > break; > endif; > endfor; > > Cheers! > > Mike

« previous php.general (#193801) next »