0

我正在使用来自不同数据库的多个 mysql 查询,从带有数组的 while 循环中提取值,并将它们重新组合成一个循环,如下所示:

$len = max(count($insrtdate),count($energy));
for($i=0;$i<$len;$i++){
    $date = isset($insrtdate[$i]) ? $insrtdate[$i] : '';
    $energy1 = isset($energy[$i]) ? $energy[$i] : '';
    echo $energy1;
}

该值$energy1最初为 0,我想检测它更改为任何数字的行以及返回初始值 0 的行。

4

1 回答 1

0

I'm not 100% sure that I understand your question, but this should be what you want:

$len = max(count($insrtdate), count($energy));
$lastEnergy1 = null;
for($i=0; $i < $len; $i++){
    $date = isset($insrtdate[$i]) ? $insrtdate[$i] : '';
    $energy1 = isset($energy[$i]) ? $energy[$i] : '';
    if ($lastEnergy1 !== $energy1) {
    $onState = ($energy1 == "1" ? "on" : "off");
        echo "Energy1 turned " . $onState . " at " . $date . "<br />";
        $lastEnergy1 = $energy1;
    }
}
于 2012-09-03T13:28:04.020 回答