feub feub - 23 days ago 6
PHP Question

Loop through an array of integers and break every around 1000

Hard to summarize what I want in the title. Here is my problem, I am working with activity tracking files, I want to browse through a GPX file and do something every 1000m of distance. But the device does not necessary record a GPS track right at 1000m, 2000m, etc, so I cannot use something like

$distance % 1000
.

Here is an example of the distance tracked :

0
3
28
36
42
66
73
80
103
125
.
.
.
997
1003
1027
.
.
.
2006
.
.
.
3001
and so on.

How can I "break" every around 1000?

Thank you.

Answer

If your data is coming in a string then you could explode the string by what ever the delimiter is and then 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 as $value )
{
  if( (int)$value > ( 1000 * $multiple ) ) 
  {
    // Passed a 1000 marker
    echo $value."\n";
    $multiple++;
  }
}

Output:

1003
2006
3001