array to real list

Very simple function, but some newbie might have use for it; could also probably be integrated with Smarty if it’s not already. Probably should add some error throwing in here, but this works for my purposes.

Enjoy:

<?php

	// turns an array into a "real list"

function array_to_real_list( $array ) {

	$str = '';

	$last_part = array_pop($array);

	foreach ( $array as $part ) {
		$str .= trim($part) . ', ';
	}

	$str .= 'and ' . $last_part;

  return $str;

}

/*	Usage example: ----------------------------------------

	$array = array('lettuce', 'tomato', 'corn', 'ranch salad dressing');

echo array_to_real_list($array); // lettuce, tomato, corn, and ranch salad dressing

----------------------------------------------------------- */
Social Bookmarks:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • Reddit
  • Technorati
  • Yahoo! Bookmarks

Leave a Reply