开发者

Converting a php array to SQL array

开发者 https://www.devze.com 2023-01-20 11:37 出处:网络
I want something like the following: $arrayOfValues = array(1,2,3,4); $sqlArray = mysql_convertToSqlArray($arrayOfValues);

I want something like the following:

$arrayOfValues = array(1,2,3,4);
$sqlArray = mysql_convertToSqlArray($arrayOfValues);

which then will return what in SQL would be:

(1,2,3,4)

but in php would be the string "(1开发者_JAVA技巧,2,3,4)"


There's no builtin function specifically for creating SQL arrays, but you could just join the array and wrap it in parentheses:

$arrayOfValues = array(1,2,3,4);
$sqlArray = '(' . join(',', $arrayOfValues) . ')';

See this in action at http://www.ideone.com/KYApN.


Take a look at http://www.php.net/manual/en/function.implode.php.
This function can be used as following: $sqlArray = "(" . implode(",", $arrayOfValues) . ")";

[Edit]
Ps: join is an alias of implode.

0

精彩评论

暂无评论...
验证码 换一张
取 消