tags:

views:

35

answers:

2

Hey,

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,2,3,4)"

+3  A: 

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.

Daniel Vandersluis
Anybody have any experience with ideone.com? At first glance it looks like jsfiddle for php...which would be bloody awesome.
treeface
@treeface it's jsfiddle for all kinds of languages. :)
Daniel Vandersluis
Cooooool /nerdgasm
treeface
+1  A: 

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.

Alxandr