Some pseudo code, I wanted to do this with Foreach if possible
I don't know if is possible to get the position from the array
$num = array ( 1 => 7,
2 => 9,
3 => 10,
4 => 11,);
foreach ($num as $value) {
if (first position) { echo $value . '<br> first <br>';}
elseif (middle position) { echo $value . '<br> middle <br>';}
elseif (last position) { echo $value . '<br> last';}
}
Expected result
7
first
9
middle
10
middle
11
last
$num = array ( 1 => 7,
2 => 9,
3 => 10,
4 => 11);
$i = 1;
foreach ($num as $value) {
if($i == 1){// if first
echo $value.'<br> first<br>';
}else if(count($num) == $i){// if last
echo $value.'<br> last';
}else{// if between first and last
echo $value.'<br> middle<br>';
}
$i++;
}
//---output
/*
7
first
9
middle
10
middle
11
last
*/
Solved on my own, thanks @chris85
$num = array ( 1 => 7,
2 => 9,
3 => 10,
4 => 11,);
$arrlen = count($num);
foreach ($num as $key=>$value) {
if ($key == 1) { echo $value . '<br> first <br>';}
elseif ($key != $arrlen) { echo $value . '<br> middle <br>';}
else { echo $value . '<br> last';}
}