List Info

Thread: note 78411 added to function.implode




note 78411 added to function.implode
user name
2007-10-10 17:42:22
Below is the function for making an English-style list from
an array, seems to be simpler than some of the other
examples I've seen.

<?php
function ImplodeProper($arr, $lastConnector = 'and')
{
  if( !is_array($arr) or count($arr) == 0) return '';
  $last = array_pop($arr);
  if(count($arr))
    return implode(', ',$arr).", $lastConnector
$last";
  else
    return $last;  
}
?>

Examples:
<?
   print ImplodeProper(array()).'<br>';
   print ImplodeProper(array('foo')).'<br>';
   print ImplodeProper(array('foo','bar')).'<br>';
   print
ImplodeProper(array('for','bar','bleh')).'<br>';
?>

Yields:

foo
foo, and bar
for, bar, and bleh
----
Server IP: 208.69.120.35
Probable Submitter: 131.216.7.191
----
Manual Page -- htt
p://www.php.net/manual/en/function.implode.php
Edit        -- https://master
.php.net/note/edit/78411
Del: integrated  -- h
ttps://master.php.net/note/delete/78411/integrated
Del: useless     -- http
s://master.php.net/note/delete/78411/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/78411/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/78411/spam
Del: non-english -- 
https://master.php.net/note/delete/78411/non-english
Del: in docs     -- http
s://master.php.net/note/delete/78411/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78411
Reject      -- https://mast
er.php.net/note/reject/78411
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )