0

I want to combine columns if current value same with next value in foreach... How to get value from foreach if current value same with next value? example:

foreach($array as $key => $value) {
  if($currvalue == $nextvalue) {
     echo "TRUE";
  } 
}
Wilq
  • 2,245
  • 4
  • 33
  • 37
Alul R
  • 41
  • 1
  • 6
  • to compare the current and next value you need to use for loop, Can you share `$array` here? – M.Hemant May 09 '19 at 08:33
  • You've tagged jQuery, yet the code in the question is PHP...? Are you wanting to do this on the server or client side? – Rory McCrossan May 09 '19 at 08:45
  • This's my array ```$array= array(["Name"=>"Peter", "Age"=>"34", "Address"=>"City A"], ["Name"=>"Ben", "Age"=>"31", "Address"=>"City A"], ["Name"=>"Joe", "Age"=>"35", "Address"=>"City B"])``` – Alul R May 09 '19 at 08:51

2 Answers2

2

Try this,

$array= array(["Name"=>"Peter", "Age"=>"34", "Address"=>"City A"], ["Name"=>"Ben", "Age"=>"31", "Address"=>"City A"], ["Name"=>"Joe", "Age"=>"35", "Address"=>"City B"]);
$asize = count($array);
echo $asize . '<pre>';
for ($i = 0; $i < $asize; $i++) {
     echo $array[$i]['Address'].'<pre>';
     if (isset($array[$i]['Address']) == isset($array[$i + 1]['Address'])) {
         if ($array[$i]['Address'] == $array[$i + 1]['Address']) {
             echo $array[$i]['Address'];
             echo " => SAME VALUE";
             echo '<pre>';
         }
    }
}
die;

Output:

3

City A

City A => SAME VALUE

City A

City B
M.Hemant
  • 2,345
  • 1
  • 9
  • 14
  • how if array => ```$array = array(["Name"=>"Peter", "Age"=>"34", "Address"=>"City A"], ["Name"=>"Ben", "Age"=>"31", "Address"=>"City A"], ["Name"=>"Joe", "Age"=>"35", "Address"=>"City B"]);```?... I want to check if current address == next address then true... – Alul R May 09 '19 at 08:57
  • not work for $array = array( ["Name"=>"Peter", "Age"=>"34", "Address"=>"City A"], ["Name"=>"Peter", "Age"=>"34", "Address"=>"City A", "city"=>"City A"], ["Name"=>"Ben", "Age"=>"31", "Address"=>"City A"], ["Name"=>"Joe", "Age"=>"35", "Address"=>"City B"], ["Name"=>"Joe", "Age"=>"35", "Address"=>"City B"] ); – Amarat May 09 '19 at 09:37
  • This is also true for if the current array count not match to next array. that is wrong. Yous should also match count of current array to next array – Amarat May 09 '19 at 09:42
  • Thanks for pointing my attention there, I assumed, the user will not change his data – M.Hemant May 09 '19 at 10:31
1

Try this :-

for($i=0; $i < count($array); $i++){
    if(isset($array[$i+1])){
        echo "<pre>"; print_r($array[$i]);
        if( $array[$i] == $array[$i+1] ){
            if ($array[$i]['Address'] == $array[$i + 1]['Address']) {
                 echo $array[$i]['Address'];
                 echo " => Same Array";
                 echo '<pre>';
            }
        }
    }
}
Amarat
  • 602
  • 5
  • 11
  • 1
    Do not use `count()` in for loop it will increase your looping time, do it before the for loop – M.Hemant May 09 '19 at 10:26
  • i don'i use count in my code. i use this if( $array[$i] == $array[$i+1] ){} Please check reference : https://stackoverflow.com/questions/5678959/php-check-if-two-arrays-are-equal Your are missing to compare both array via if( $array[$i] == $array[$i+1] ){} – Amarat May 09 '19 at 10:36
  • you have `count()` in `$i < count($array);` – M.Hemant May 09 '19 at 10:53
  • this is similar for $asize = count($array); and use it in for loop like for ($i = 0; $i < $asize; $i++) { } i directly used for($i=0; $i < count($array); $i++){ } both are same. Please check reference link to caompare both array: - https://stackoverflow.com/questions/5678959/php-check-if-two-arrays-are-equal – Amarat May 09 '19 at 10:57