duagfgfn1981
2011-12-29 01:47
浏览 43
已采纳

如何将字符串数组写入单个格式化的字符串中

I have an array of items:

array(
    'apples',
    'oranges',
    'pineapples'
)

that I would like to format like so: "Some of my favorite fruits are apples, oranges and pineapples"

So, using PHP I would like to be able to transform that array into a reader friendly apples, oranges and pineapples.

At first I thought I might be able to use array_map but i'm not sure how I would go about telling what the last and 2nd to last items are (it's easy to add a , after each item with array_map, however you wouldn't want a comma before the last item)

Thoughts?

图片转代码服务由CSDN问答提供 功能建议

我有一个项目数组:

  array(\  n'apples',
'oranges',
'spineapples'
)
   
 
 

我想格式化如下:“我最喜欢的一些 水果是 apples oranges pineapples

所以,使用 PHP 我希望能够将该数组转换为读者友好的 apple,oranges和pineapples

起初我以为我可以使用< code> array_map 但是我不知道如何判断最后和第二个项目是什么(使用 array_map在每个项目之后添加很容易 ,但是你不想在最后一项之前使用逗号)

思考?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dsxi70423 2011-12-29 01:49
    已采纳

    Divide the problem to conquer it: Take out the last element, create the comma separated list and then add the last element.

    $array = array(
        'apples',
        'oranges',
        'pineapples'
    );
    
    $last = array_pop($array);
    echo implode(', ', $array), ' and ', $last; # apples, oranges and pineapples
    

    Naturally this only makes sense when there are at least two values in the array. Functions used: implodeDocs (probably more suitable than array_map in this case) and array_popDocs.


    On the other hand, if the values do not contain any , you can first create a comma separated string with the implode function and then use a regular expression to replace the last comma with and.

    That done will also work on arrays that have less than 2 elements. I do this in a loop that removes one element from the array in each step so that it shows how it behaves:

    $array = array(
        'apples',
        'oranges',
        'pineapples'
    );
    
    foreach($array as $v)
    {
        echo preg_replace('(,([^,]*)$)', ' and$1', implode(', ', $array)), "
    ";
        array_pop($array);
    }
    

    Output:

    apples, oranges and pineapples
    apples and oranges
    apples
    
    已采纳该答案
    打赏 评论
  • doujiang7258 2011-12-29 01:50

    Perhaps your looking for implode, which will essentially turn an array to string

    打赏 评论
  • drvc63490 2011-12-29 02:16

    You can know which one is the last element by counting them:

    $array = array(
        'apples',
        'oranges',
        'pineapples'
    );
    
    $count= count($array);
    // because the key start from 0 , we must subtract 1 
    $lastKey= $count-1;
    
    
    /// now u can do that
    echo "<br>1st :".$array[0];
    echo "<br>2nd :".$array[1];
    echo "<br>3rd :".$array[2];
    echo "<br>last:".$array[$lastKey];
    
    // or same as u like
    
    foreach($array as $key=>$value){
        echo $value;
        if($key == $lastKey){
            echo ".";
        }elseif($key == ($lastKey-1)){
            echo " and ";
        }else{
            echo " , ";
        }
    }
    
    打赏 评论
  • dongqiang8058 2011-12-29 02:28

    Ok, so as others have commented implode is a solution.

    I would probably end up using a combination of implode, strripos and substr_replace:

    $string = implode(', ', $fruit); 
    $pos = strripos($string, ', ');
    echo substr_replace($string, ' and ', $pos, 2);
    

    which results in apples, oranges and pineapples.

    I'd gladly post a more elegant solution if I could think of one, but this is a quick and easy way to get at the change you need.

    打赏 评论

相关推荐 更多相似问题