note 80381 added to function.implode

From: Date: Sun, 13 Jan 2008 04:00:09 +0000
Subject: note 80381 added to function.implode
Groups: php.notes 
Request: Send a blank email to php-notes+get-135063@lists.php.net to get a copy of this message
My take on a simple english list function - it's similar to what's already here, but calls count less function x_implode_eng_list($array, $glue=', ', $final=' and '){ //takes an array and outputs an English style list //perform the count once $count = count($array); //make sure it's an array and has content if((!is_array($array)) || ($count == 0)) { return ''; } else { //pop off the end regardless of length $end = array_pop($array); //if there's more than one, implode the remainder using glue and append $end if($count>1){ return implode($glue, $array) . $final . $end; } else { //only one value, return the popped end element return $end; } } } $names[] = 'bob'; $names[] = 'john'; $names[] = 'lucy'; echo x_implode_eng_list($names); //will output bob, john and lucy echo x_implode_eng_list($names, ', ', ' or '); //will output bob, john or lucy ---- Server IP: 87.124.64.96 Probable Submitter: 212.84.120.6 ---- Manual Page -- http://www.php.net/manual/en/function.implode.php Edit -- https://master.php.net/note/edit/80381 Del: integrated -- https://master.php.net/note/delete/80381/integrated Del: useless -- https://master.php.net/note/delete/80381/useless Del: bad code -- https://master.php.net/note/delete/80381/bad+code Del: spam -- https://master.php.net/note/delete/80381/spam Del: non-english -- https://master.php.net/note/delete/80381/non-english Del: in docs -- https://master.php.net/note/delete/80381/in+docs Del: other reasons-- https://master.php.net/note/delete/80381 Reject -- https://master.php.net/note/reject/80381 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135063) next »