Generate Array from a comma-separated list - PHP

前端 未结 3 1392
[愿得一人]
[愿得一人] 2020-12-19 00:23

I have a variable defined like so: $var = \"1, 2, 3\"; & I have an array: $thePostIdArray = array(1, 2, 3);

The Array above wo

3条回答
  •  心在旅途
    2020-12-19 01:01

    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
    

提交回复
热议问题