[英]How implode correctly this array?
I have this array ( $recip
): 我有这个数组( $recip
):
Array
(
[0] => 393451234567
[1] => 393479876543
)
SMS API provider requires numbers in this format: SMS API提供程序要求使用以下格式的数字:
recipients[]=393334455666&recipients[]=393334455667
With 同
$recipients = implode('&recipients[]=',$recip);
I can obtain only this: 我只能得到这个:
393471234567&recipients[]=393459876543
Missing first one " recipients[]
" (overall, first one no require the " &
" at all). 缺少第一个“ recipients[]
”(总的来说,第一个完全不需要“ &
”)。
只需将初始recipients[]=
附加到字符串的前面即可:
$recipients = 'recipients[]=' . implode('&recipients[]=',$recip);
Another option: 另外一个选项:
foreach ($array as $key => $value){
$array[$key] = (($key == 0) ? '' : '&').'recipients[]='.$value;
}
$result = implode('',$array);
A foreach
loop allows you to conctenate your string. foreach
循环使您可以简化字符串。 I include a check to avoid appending the & on the first part of the string. 我提供了一个检查以避免在字符串的第一部分附加&。
Pointing this out as an option, but the other way is simpler! 指出这一点是一种选择,但是另一种方式更简单!
尝试这个:
vsprintf('recipients[]=%s&recipients[]=%s', $recip);
Another option 另外一个选项
foreach ($recip as $ip){
$array[] = 'recipients[]=' . $ip;
}
$result = implode('&',$array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.