0

I am working on taxi app, after ride completes i have to draw map for which i use google map apis. let suppose my way-points are

array($coordinates1,$coordinates2,$coordinates3,$coordinates4,$coordinates5,$coordinates6,.........,$coordinates40)

the problem is that google map api accept only maximum of 23 way-points, for this purpose I have to skip many coordinates to pass, here my idea is to unset elements of array with equal intervals so that the route remain same. Please someone guide me how to remove many number of elements from an array with equal interval, just like i need 23 and total elements are 40 so that i need like

array($coordinates1,$coordinates4,$coordinates7,$coordinates9,$coordinates13,$coordinates16,.........,$coordinates39)

Aftab Ahmad
  • 354
  • 1
  • 2
  • 16
  • Possible duplicate of [PHP: Delete an element from an array](https://stackoverflow.com/questions/369602/php-delete-an-element-from-an-array) – Jonny Apr 02 '18 at 16:44
  • i want to delete many element with equal interval depend on number of elements in array, not a single element – Aftab Ahmad Apr 02 '18 at 16:55

1 Answers1

0

I have found a possible way, its a little bit lengthy but i got exact required result

$required_elements = 23;
$way = array();
for($ini=1;$ini<=78;$ini++)
{
    $way[] = "Co-Ordinated:".$ini;
}
$total_elements = count($way);
list($way1, $way2) = array_chunk($way, ceil(count($way) / 2));
$differrence_element = $total_elements-$required_elements;
$skip_element = ceil($total_elements/$differrence_element);
$output = array();
if($total_elements > $required_elements)
{
    $i=1;$j=0;
    foreach($way as $x)
    {
        if($i == $skip_element)
        {
            $i=1;$j++;
            continue;
        }
        else
        {
            $output[] = $way1[$j];
            if(count($output) >= $required_elements) break;
            $output[] = $way2[$j];
            if(count($output) >= $required_elements) break;
            $i++;$j++;
        }
    }
}
else
{
    $output = $way;
}
$output1 = array();
$output2 = array();
$i=0;
foreach($output as $g)
{
    if($i%2 == 0)
        $output1[] = $g;
    else
        $output2[] = $g;
    $i++;
}

$result = array_merge($output1,$output2);

print_r($way);
echo "<br><br><pre>";
print_r($result);
echo '</pre>';
Aftab Ahmad
  • 354
  • 1
  • 2
  • 16