note 71346 deleted from function.implode by hholzgra

From: Date: Thu, 02 Aug 2012 13:17:38 +0000
Subject: note 71346 deleted from function.implode by hholzgra
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189736@lists.php.net to get a copy of this message
Note Submitter: richard at happymango dot me dot uk ---- This is a simple function that is the same as implode except it allows you to specify two glue parameters instead of one so an imploded array would output "this, this, this and this" rather than "this, this, this, this, this". This is useful if you want to implode arrays into a string to echo as part of a sentence. It uses the second glue between the last two items and the first glue between all others. It will use the second glue if there are only two items to implode so it would output "this and this". <?php function implode2($glue1, $glue2, $array) { return ((sizeof($array) > 2)? implode($glue1, array_slice($array, 0, -2)).$glue1 : "").implode($glue2, array_slice($array, -2)); } //example below $array = array("Monday", "Tuesday"); echo "1: ".implode2(', ', ' and ', $array)."<br />"; $array = array("Mon", "Tue", "Wed", "Thu", "Fri"); echo "2: ".implode2(', ', ' &amp; ', $array)."<br />"; $array = array( 1, 2, 3, 4, 10); echo "3: ".implode2(' + ', ' = ', $array)."<br />"; ?> This outputs 1: Monday and Tuesday 2: Mon, Tue, Wed, Thu & Fri 3: 1 + 2 + 3 + 4 = 10

« previous php.notes (#189736) next »