这个问题是初等编程,在大学的第一周你通常会遇到这样的问题。
array_diff 不会帮助你,它比较两个不同的数组,而你正在寻找一个数组中前一个元素的比较。
无论是来自 mysql 还是数组,同样的原则都适用。
另外,问自己一个问题,是否需要先对这些数据进行排序?
尝试这个
<?php
$my_array = array(1900,1900,2000);
$tmp = null ;
for( $i = 0 ; $i<count($my_array) ; $i++) {
if( $my_array[$i] != $tmp ){
echo "\nValue $i is different to the last one,
should I do extra stuff here ?";
} else {
echo "\nValue $i is the same as the last one and equals $tmp";
}
$tmp = $my_array[$i] ;
}
?>
上面代码的输出是..
Value 0 is different to the last one, should I do extra stuff here ?
Value 1 is the same as the last one and equals 1900
Value 2 is different to the last one, should I do extra stuff here ?
确定动作是否发生在第一个条目上很简单,检查 $i 的值