View Single Post
  #3  
Old 08-19-2006, 10:43 AM
stephanie stephanie is offline
OSP Starters
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
stephanie is on a distinguished road
Default

This'll save you a loop:


PHP Code:
//initial array
$array1 = array('bob');
$array2 = array('bob','martha');
$array3 = array('bob','martha','jim');

$count = count($array);

//implode will ignore the glue in single-element arrays,
//so we can run arrays 1 and 2 through the basic 'and' routine
if( $count < 3 )
{
$string = implode(' and ',$array);
}
else
{
//break array into comma-delimited string
$string = implode(', ',$array);

//get position of comma before the last array element
$spot = strrpos($string, ', ' . $array[$count - 1]);

//replace the ', ' before the last element with ' and '
$string = substr_replace($string, ' and ', $spot, 2);

}

//$array1 = 'bob'
//$array2 = 'bob and martha'
-- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
  Webmaster Forums - View Single Post - Variable, variables and variable
View Single Post
  #3  
Old 08-19-2006, 10:43 AM
stephanie stephanie is offline
OSP Starters
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
stephanie is on a distinguished road
Default

This'll save you a loop:


PHP Code:
//initial array
$array1 = array('bob');
$array2 = array('bob','martha');
$array3 = array('bob','martha','jim');

$count = count($array);

//implode will ignore the glue in single-element arrays,
//so we can run arrays 1 and 2 through the basic 'and' routine
if( $count < 3 )
{
$string = implode(' and ',$array);
}
else
{
//break array into comma-delimited string
$string = implode(', ',$array);

//get position of comma before the last array element
$spot = strrpos($string, ', ' . $array[$count - 1]);

//replace the ', ' before the last element with ' and '
$string = substr_replace($string, ' and ', $spot, 2);

}

//$array1 = 'bob'
//$array2 = 'bob and martha'
-- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote