note 71346 added to function.implode

From: Date: Thu, 23 Nov 2006 12:43:35 +0000
Subject: note 71346 added to function.implode
Groups: php.notes 
Request: Send a blank email to php-notes+get-120022@lists.php.net to get a copy of this message
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 ---- Server IP: 83.138.144.80 Probable Submitter: 80.194.36.8 ---- Manual Page -- http://www.php.net/manual/en/function.implode.php Edit -- https://master.php.net/note/edit/71346 Del: integrated -- https://master.php.net/note/delete/71346/integrated Del: useless -- https://master.php.net/note/delete/71346/useless Del: bad code -- https://master.php.net/note/delete/71346/bad+code Del: spam -- https://master.php.net/note/delete/71346/spam Del: non-english -- https://master.php.net/note/delete/71346/non-english Del: in docs -- https://master.php.net/note/delete/71346/in+docs Del: other reasons-- https://master.php.net/note/delete/71346 Reject -- https://master.php.net/note/reject/71346 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120022) next »