implode()字符串,但最后还附加了胶水

Rya*_*ler 5 php string implode

尝试使用该implode()函数在每个元素的末尾添加一个字符串.

$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("@txt.att.net,", $array);

print($attUsers);
Run Code Online (Sandbox Code Playgroud)

打印这个:

9898549130@txt.att.net,9898549131@txt.att.net,9898549132
Run Code Online (Sandbox Code Playgroud)

我怎样才能implode()为最后一个元素附加胶水?

预期产量:

9898549130@txt.att.net,9898549131@txt.att.net,9898549132@txt.att.net
                                                      //^^^^^^^^^^^^ See here
Run Code Online (Sandbox Code Playgroud)

aal*_*aap 6

有一种更简单、更好、更有效的方法可以使用array_maplambda 函数来实现这一点:

$numbers = ['9898549130', '9898549131', '9898549132'];

$attUsers = implode(
    ',',
    array_map(
        function($number) {
            return($number . '@txt.att.net');
        },
        $numbers
    )
);

print_r($attUsers);
Run Code Online (Sandbox Code Playgroud)


Rya*_*ler 1

这是我朋友的回答,它似乎提供了使用 foreach 的最简单的解决方案。

$array = array ('1112223333', '4445556666', '7778889999');

// Loop over array and add "@att.com" to the end of the phone numbers
foreach ($array as $index => &$phone_number) {
    $array[$index] = $phone_number . '@att.com';
}

// join array with a comma
$attusers = implode(',',$array);  

print($attusers); 
Run Code Online (Sandbox Code Playgroud)