Last Item in Array

Useful to add custom styles or punctuation to last item in an array.

$lastElement = array_key_last($subjects);
$count = 0;

foreach($subjects as $sub) { 
		if($count == $lastElement) {
			echo $sub->name;	
		} else {
			echo $sub->name .', ';
	}
	
	$count++;
} ?></li>

Source.

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.