I want to detect the first and the last element inside a foreach loop:

```
$path = "monkey/cat/horse";

foreach(explode('/', $path) as $segment) {
   echo $segment;
}

```

My desired result is:

```
first:monkey
last:horse

```

This is how I tried to solve it:

```
$path = "monkey/cat/horse";
$explode = explode('/', $path);

foreach($explode as $segment) {
    if (current($explode) == $segment){
        echo "first:".$segment;
    } echo "<br>";
    if (end($explode) == $segment){
        echo "last:".$segment;
    }
}

```

But my result is:

```
first:horse
last:horse

```