I have a variable defined like so: $var = \"1, 2, 3\"; & I have an array: $thePostIdArray = array(1, 2, 3);
The Array above wo
Check out explode: $thePostIdArray = explode(', ', $var);
For developer who wants result with and
in the end can use the following code:
$titleString = array('apple', 'banana', 'pear', 'grape');
$totalTitles = count($titleString);
if($totalTitles>1)
{
$titleString = implode(', ' , array_slice($titleString,0,$totalTitles-1)) . ' and ' . end($titleString);
}
else
{
$titleString = implode(', ' , $titleString);
}
echo $titleString; // apple, banana, pear and grape
use explode function. this will solve your problem. structure of explode is like this
array explode ( string $delimiter , string $string [, int $limit ] )
now $delimiter
is the boundary string, string $string
is the input string.
for limit:
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
If the limit parameter is negative, all components except the last -limit are returned.
If the limit parameter is zero, then this is treated as 1.
visit the following link. you can learn best from that link of php.net