Yes, arrays are a great way to reduce the code when you have several things that need to react in the same way.
I just compressed my 400 lines for the rollers down to 69 lines thanks to arrays.
Code: Select all
<?php $kwartier=870;$tweeuur=7170;$msg='Rolluiken__';if($buien>=30)$open=31;else$open=0;
$items=array('Rliving','Rbureel','RkeukenL','RkeukenR','Rtobi','Ralex','RkamerL','RkamerR','luifel','raamliving','raamtobi','raamalex','raamkamer','zonOP','buiten_temp','living_temp','tobi_temp','alex_temp','kamer_temp','heatingauto','buien','wind','modeRliving','modeRbureel','modeRkeukenL','modeRkeukenR','modeRtobi','modeRalex','modeRkamerL','modeRkamerR');foreach($items as $i)${$i}=apcu_fetch($i);
$items=array('Rliving','Rbureel','RkeukenL','RkeukenR','Rtobi','Ralex','RkamerL','RkamerR','raamtobi','raamalex','raamkamer');foreach($items as $i)${'T'.$i}=past($i);
if(time<strtotime('7:00')||time>=strtotime('22:00'))$nacht=true;else $nacht=false;
if(time>=strtotime('7:00')&&time<strtotime('8:30'))$ochtend=true;else $ochtend=false;
if(time>=strtotime('8:30')&&time<strtotime('16:30'))$dag=true;else $dag=false;
if(time>=strtotime('16:30')&&time<strtotime('22:00'))$avond=true;else $avond=false;
$boven=array('Rtobi','Ralex','RkamerL','RkamerR');$beneden=array('Rbureel','RkeukenL','RkeukenR');$benedena=array('Rliving','Rbureel','RkeukenL','RkeukenR');
if($heatingauto=='On'){$msg.='Winter__';
if($Weg==0){$msg.='Thuis__';
if($nacht){$msg.='Nacht__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
foreach($boven as $i) {if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
}elseif($ochtend){$msg.='Ochtend__';
if($zonOP&&$zon==0){$msg.='ZonOP && Zon = 0__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
}elseif($zonOP){$msg.='ZonOP && Zon = '.$zon.'__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
foreach($boven as $i){if($i=='Rtobi')$raam=$Traamtobi;elseif($i=='Ralex')$raam=$Traamalex;else$raam=$Traamkamer;
if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier&&$$raam<$tweeuur){sl($i,$open);$msg.=$i.' '.$open.'__';}
}
}
}elseif($dag){$msg.='Dag__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
foreach($boven as $i){if($i=='Rtobi')$raam=$Traamtobi;elseif($i=='Ralex')$raam=$Traamalex;else$raam=$Traamkamer;
if(${'mode'.$i}&&${$i}>$open&&${'T'.$i}>$kwartier&&$raam<$tweeuur){sl($i,$open);$msg.=$i.' '.$open.'__';}
}
}elseif($avond){$msg.='Avond__';
if($zonOP&&$zon==0){$msg.='zonOP && Zon = 0__';
foreach($beneden as $i){if(${'mode'.$i}&&${$i}<$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
}elseif($zonOP&&$zon<50){$msg.='zonOP && Zon < 50 : '.$zon.'__';
foreach($boven as $i){if(${'mode'.$i}&&${$i}<$open&&${'T'.$i}>$kwartier){sl($i,100);$msg.=$i.' Dicht__';}}
}elseif($zonOP){$msg.='zonOP, zon = '.$zon.'__';
}else{$msg.='Zononder __';
foreach($beneden as $i){if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
foreach($boven as $i){if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
}
}
}elseif($Weg==1){$msg.='Slapen__';
foreach($boven as $i) {if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
foreach($benedena as $i){if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
}elseif($Weg==2){$msg.='Weg__';
if($nacht){$msg.='Nacht__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
foreach($boven as $i) {if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
}elseif($ochtend){$msg.='Ochtend__';
if($zonOP&&$zon==0){$msg.='ZonOP && Zon = 0__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
}elseif($zonOP){$msg.='ZonOP && Zon = '.$zon.'__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
foreach($boven as $i){if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
}
}elseif($dag){$msg.='Dag__';
foreach($benedena as $i){if(${'mode'.$i}&&${$i}!=$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
foreach($boven as $i){if(${'mode'.$i}&&${$i}>$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
}elseif($avond){$msg.='Avond__';
if($zonOP&&$zon==0){$msg.='zonOP && Zon = 0__';
foreach($beneden as $i){if(${'mode'.$i}&&${$i}<$open&&${'T'.$i}>$kwartier){sl($i,$open);$msg.=$i.' '.$open.'__';}}
}elseif($zonOP&&$zon<50){$msg.='zonOP && Zon < 50 : '.$zon.'__';
foreach($boven as $i){if(${'mode'.$i}&&${$i}<$open&&${'T'.$i}>$kwartier){sl($i,100);$msg.=$i.' Dicht__';}}
}elseif($zonOP){$msg.='zonOP, zon = '.$zon.'__';
}else{$msg.='Zononder __';
foreach($beneden as $i){if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
foreach($boven as $i){if(${'mode'.$i}&&${$i}<75){sl($i,100);$msg.=$i.' Dicht__';}}
}
}
}
}
lg(str_replace('__',' | ',$msg));if(strlen($msg)>80)telegram(str_replace('__',PHP_EOL,$msg));?>