implode() 字符串,但也在末尾附加胶水
implode() string, but also append the glue at the end
尝试使用implode()
函数在每个元素的末尾添加一个字符串。
$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("@txt.att.net,", $array);
print($attUsers);
打印这个:
9898549130@txt.att.net,9898549131@txt.att.net,9898549132
如何让 implode()
也为最后一个元素附加粘合?
预期输出:
9898549130@txt.att.net,9898549131@txt.att.net,9898549132@txt.att.net
//^^^^^^^^^^^^ See here
有一个简单的解决方案可以实现这一点:
$i = 1;
$c = count($array);
foreach ($array as $key => $val) {
if ($i++ == $c) {
$array[$key] .= '@txt.att.net';
}
}
这似乎可行,但不确定这是最好的方法:
$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("@txt.att.net,", $array) . "@txt.att.net";
print($attUsers);
$result = '';
foreach($array as $a) {
$result = $result . $a . '@txt.att.net,';
}
$result = trim($result,',');
这是我朋友的回答,似乎提供了使用 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);
有一种更简单、更好、更有效的方法可以使用 array_map
和 lambda 函数来实现:
$numbers = ['9898549130', '9898549131', '9898549132'];
$attUsers = implode(
',',
array_map(
function($number) {
return($number . '@txt.att.net');
},
$numbers
)
);
print_r($attUsers);
在内爆之前向您的数组附加一个空字符串。
但是我们还有另一个问题,末尾有一个逗号。
所以,remove it.
输入:
$array = array('9898549130', '9898549131', '9898549132', '');
$attUsers = implode("@txt.att.net,", $array);
$attUsers = rtrim($attUsers, ",")
输出:
9898549130@txt.att.net,9898549131@txt.att.net,9898549132@txt.att.net
尝试使用implode()
函数在每个元素的末尾添加一个字符串。
$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("@txt.att.net,", $array);
print($attUsers);
打印这个:
9898549130@txt.att.net,9898549131@txt.att.net,9898549132
如何让 implode()
也为最后一个元素附加粘合?
预期输出:
9898549130@txt.att.net,9898549131@txt.att.net,9898549132@txt.att.net
//^^^^^^^^^^^^ See here
有一个简单的解决方案可以实现这一点:
$i = 1;
$c = count($array);
foreach ($array as $key => $val) {
if ($i++ == $c) {
$array[$key] .= '@txt.att.net';
}
}
这似乎可行,但不确定这是最好的方法:
$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("@txt.att.net,", $array) . "@txt.att.net";
print($attUsers);
$result = '';
foreach($array as $a) {
$result = $result . $a . '@txt.att.net,';
}
$result = trim($result,',');
这是我朋友的回答,似乎提供了使用 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);
有一种更简单、更好、更有效的方法可以使用 array_map
和 lambda 函数来实现:
$numbers = ['9898549130', '9898549131', '9898549132'];
$attUsers = implode(
',',
array_map(
function($number) {
return($number . '@txt.att.net');
},
$numbers
)
);
print_r($attUsers);
在内爆之前向您的数组附加一个空字符串。
但是我们还有另一个问题,末尾有一个逗号。
所以,remove it.
输入:
$array = array('9898549130', '9898549131', '9898549132', '');
$attUsers = implode("@txt.att.net,", $array);
$attUsers = rtrim($attUsers, ",")
输出:
9898549130@txt.att.net,9898549131@txt.att.net,9898549132@txt.att.net