如果当前值与foreach中的下一个值相同,我想合并列... 如果当前值与下一个值相同,如何从foreach中获取值? 示例:
foreach($array as $key => $value) {
if($currvalue == $nextvalue) {
echo "TRUE";
}
}
答案 0 :(得分:2)
尝试一下
$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;
输出:
3
City A
City A => SAME VALUE
City A
City B
答案 1 :(得分:1)
尝试一下:-
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>';
}
}
}
}