如何删除foreach循环中的最后一个值

I have a dynamic multi input. When submit form, inserting array values. But I want to insert without 'submit' value. This is the foreach loop, (sql secure not included)

if (isset($_POST['submit'])) {
    $nearest = $db->prepare('insert into nearest set place=?, distance=?');
    $i = 0;
    foreach ($_POST as $val) {
        $place = $_POST['place'][$i];
        $distance = $_POST['distance'][$i];
        $nearest->execute([$place, $distance]);
        $i++;
    }
}

This loop inserted '$_POST' values and inserted empty row.

If you want to keep the loop you can use array_slice to not include the last item in the loop.
Array_slice will take items from 0 (not literal, but the first item) to second to last (-1).
http://php.net/manual/en/function.array-slice.php

EDIT; I think you need a for loop to loop the count of "place".

if (isset($_POST['submit'])) {
  $nearest= $db -> prepare('insert into nearest set place=?, distance=?');
  for($i=0; $i< count($_POST['place']; $i++){
      $place = $_POST['place'][$i];
      $distance= $_POST['distance'][$i];
      $nearest-> execute([$place , $distance]);
      $i++;
  }
}

example: https://3v4l.org/F47ui

You can simply remove your submit key from $_POST prior doing your loop with just regular unset(). But this is bad approach. What I'd rather recommend doing instead is to "isolate" your data, and instead of this:

<input name="distance" ... >

make all your imputs like this:

<input name="data[distance]" ... >

then you just need to loop over "data" (name as you like) array:

foreach($_POST['data'] as val)

Also, resist from removing last element in blind, because it's based on false assumption that this is always your "submit" element. What if you add more submit buttons to the form? or the order will change?