Reputation: 51
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";
}
}
Upvotes: 0
Views: 284
Reputation: 2355
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
Upvotes: 2
Reputation: 630
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>';
}
}
}
}
Upvotes: 1