[英]PHP str_replace array
i had a problem about returning an array into a spesified string. 我有一个关于将数组返回到spesified字符串的问题。 so, this is my code that i had been try.
所以,这是我一直尝试的代码。
$setValues = array("test1", "test2", "testurl");
$change = join("','", $setValues);
$done = str_replace("", "?", $change);
so, the $change variable will return an array something like this : 所以,$ change变量将返回一个如下所示的数组:
'test1', 'test2', 'testurl'
i want to change that array into something like this : 我想将该数组更改为以下内容:
?, ?, ?
is it possible to doing this? 这样做有可能吗?
use array_map() with callback 将array_map()与回调一起使用
$setValues = array("test1", "test2", "testurl");
$change = array_map(function($val) { return "?"; }, $setValues);
$change = join(",", $change);
echo $change;// outputs => ?,?,?
This will work for you- 这对你有用 -
function myfunction($num)
{
return '?';
}
$setValues = array("test1", "test2", "testurl");
$setValues = array_map("myfunction",$setValues);
$change = join("','", $setValues);
$done = str_replace("", "?", $change);
Iterating with array_map
seems way too overkill for this. 使用
array_map
迭代似乎太过分了。 You're basically trying to generate n
questionmarks, given that n = count($array)
. 考虑到
n = count($array)
,你基本上是在尝试生成n
问号。 So why not just: 那么为什么不呢:
$questionmarks = join( "," , array_fill( 0, count( $array ), "?" ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.