0

Is there a way to easily check if the values of multiple variables are equal? For example, in the code below I have 10 values, and it's cumbersome to use the equal sign to check if they're all equal.

 <?
  foreach($this->layout as $l): 
  $long1=$l['long1_campaignid'];    
  $long2=$l['long2_campaignid']; 
  $long3=$l['long3_campaignid'];    
  $long4=$l['long4_campaignid']; 
  $long5=$l['long5_campaignid'];    
  $long6=$l['long6_campaignid'];    
  $long7=$l['long7_campaignid'];    
  $long8=$l['long8_campaignid'];    
  $long9=$l['long9_campaignid'];    
  $long10=$l['long10_campaignid'];
  endforeach;
 ?>

for example if $long1=3,$long2=7,$long3=3,$long4=7,$long5=3 etc,

i need to retrieve $long1=$long3=$long5 and $long2=$long4

3
  • So you want to write $long1 == $long2 == $long3 == $long4 == $long5 == $long6 == $long7 == $long8 == $long9 == $long10 more succinctly? Commented Jun 26, 2012 at 1:37
  • If you explain more about the problem it might have a different solution than the one you are thinking of Commented Jun 26, 2012 at 1:42
  • Can you clarify what you mean by "retrieve $long1=$long3=$long5"? I understand that these three variables are equal in value, but I don't understand what result you're trying to get from them. Commented Jun 26, 2012 at 4:27

3 Answers 3

1

I think this is what you're looking for:

<?
  foreach($this->layout as $l): 
  $m = array_unique($l);
  if (count($m) === 1) {
      echo 'All of the values are the same<br>';
  }
  endforeach;
?>

I assuming that you are looking to see if all of the values in the array are the same. So to do this I call array_unique() to remove duplicates from the array. If all of the values of the array are the same this will leave us with an array of one element. So I check for this and if it is true then all of the values are the same.

Sign up to request clarification or add additional context in comments.

2 Comments

Thans for your reply, but it's not what I am looking for.if $long1=3,$long2=7,$long3=3,$long4=7,$long5=3 etc, i need to retrieve $long1=$long3=$long5 and $long2=$long4
Then you will need to do a better job explaining what you are looking for
1

The example showed at the question is about "grouping" not directly about "find equal variables".

I think this simple "grouping without change the order" algorithm is the answer... Other algorithms using sort() are also easy to implement with PHP.

<?
  foreach($this->layout as $l) {
     $m = array();
     foreach($1 as $k=>$v) // keys 'longX' and values
        if (array_key_exists($v,$m)) $m[$v][] = $k; 
        else                         $m[$v]   = array($k);
     foreach ($m as $val=>$keys)
        if (count($keys)>1) echo "<br/> have SAME values: ".join(',',$keys)
  }
?>

About "find equal variables"

Another (simple) code, see Example #2 at PHP man of array_unique.

<?
  $m = array_unique($this->layout);
  $n = count($m);
  if ($n == 1)
      echo 'All of the values are the exactly the same';
  elseif ($n>0)
      echo 'Different values';
  else 
      echo 'No values';
?>

The "equal criteria" perhaps need some filtering at strings, to normalize spaces, lower/upper cases, etc. Only the use of this "flexible equal" justify a loop. Example:

<?
  $m = array();
  foreach($this->layout as $l) 
      $m[trim(strtolower($1))]=1;
  $n = count(array_keys($m));
  if ($n == 1)
      echo 'All of the values are the exactly the same';
  elseif ($n>0)
      echo 'Different values';
  else 
      echo 'No values';
?>

Comments

0

If @John Conde's answer is not what you are looking for and you want to check for one or more of the same values in the collection, you could sort the array and then loop through it, keeping the last value and comparing it to the current value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.