Location: PHPKode > scripts > FBCount > fbcount/fbcount.class.php
<?php

/**
 * FBCount PHP Class
 * by Nemanja Avramovic, www.avramovic.info
 * Published under LGPL license
 *
 * Use FBCount:asString($array) to get human-friendly string from array.
 *
 * Dedicated to my beloved brother FILIP. Rest in peace!
 */

class FBCount {

	function array_is_associative ($array)
	{
		if ( is_array($array) && ! empty($array) )
		{
			for ( $iterator = count($array) - 1; $iterator; $iterator-- )
			{
				if ( ! array_key_exists($iterator, $array) ) { return true; }
			}
			return ! array_key_exists(0, $array);
		}
		return false;
	}

	function lastKey($niz) {
		end($niz);
		return key($niz);
	}

	function firstKey($niz) {
		reset($niz);
		return key($niz);
	}

	function asString($niz, $veznik = 'and', $format = '%s') {

		if ($veznik == NULL) { $veznik =  'and'; }

		if (!is_array($niz)) {
			return $niz;
		}

		if (count($niz) < 1) {
			return false;
		}
		else {
			if (!FBCount::array_is_associative($niz)) {

				if (count($niz) > 1) {
					$poslednji = $niz[count($niz)-1];
					unset($niz[count($niz)-1]);
					$str = '';
					foreach ($niz as $value) {
						$str .= sprintf($format, $value);
						if ($value != $niz[count($niz)-1]) $str .= ', ';
					}
					$str .= ' '.$veznik.' ';
					$str .= sprintf($format, $poslednji);
					return $str;
				}
				else {
					return sprintf($format, $niz[0]);
				}
			}
			else {
				if (count($niz) > 1) {
					$poslednji = end($niz);
					$poslednjikey = FBCount::lastKey($niz);
					unset($niz[$poslednjikey]);

					$str = '';
					foreach ($niz as $key=>$value) {
						$str .= sprintf($format, $key, $value);
						if (($key != FBCount::lastKey($niz)) && ($value != end($niz))) $str .= ', ';
					}
					$str .= ' '.$veznik.' ';
					$str .= sprintf($format, $poslednjikey, $poslednji);
					return $str;

				}
				else {
					return sprintf($format, FBCount::firstKey($niz), reset($niz));
				}
			}
		}

	}

}

?>
Return current item: FBCount