php - Loop through an array of integers and break every 1000 -
i working activity tracking files. want browse through gpx file , every 1000m milestone. device not record gps track right @ 1000m, 2000m, etc, cannot use $distance % 1000
. want detect each time 1000m milestone has passed, performing action @ milestone after.
here example of distance tracked:
0 3 28 … 997 1003 1027 … 1998 2006 … 2989 3001
and on. in example, i'd need perform action when 1003 has been reached, 2006 , 3001.
how can trigger event every time data passes multiple of 1000?
if data coming in string explode string ever delimiter , loop it.
<?php $string = "0 3 28 36 42 66 73 80 103 125 997 1003 1027 2006 3001"; $data= explode( ' ', $string ); $multiple = 1; foreach( $data $value ) { if( (int)$value > ( 1000 * $multiple ) ) { // passed 1000 marker echo $value."\n"; $multiple++; } }
output:
1003 2006 3001
Comments
Post a Comment