Hey all,

Im a little confused about taking the values of a 2D array and placing them into a query or string to use in query. I've done some homework but unfortunatly square eyes and a caffeine comedown just isn't helping.

Essentially I have an array named $myarray.
It looks like this:
Array ( [1] => 3 [2] => 4 [3] => 3 )
What I'd like to do is take the 3,4,3, and be able to use in a sql query such as "INSERT INTO table(column) VALUES (3, 4, 3)";

Any help will be welcomed as most sites I've checked refer to using implode on 1D arrays.

If you want to insert into different columns different values then try the following.

$array=array('column1'=>'value1', 'column2'='value2', 'column'='another');
$sql='INSERT INTO `table` SET';
foreach ($array AS $key => $val) {
$sql.=' `'.mysql_real_escape_string($key).'` = "'.mysql_real_escape_string($val).'",';
}
$sql=substr($sql,0,-1);

echo $sql;

Hey cwarn23,

Thanks for your reply. I took on board your code, however, went with something like this instead.

while($row= mysql_fetch_array($result)){
			$myarray= $row[1];
			$text.=(" '".$myarray."',");
			}

Appreciate the input. As I'm failry new to php it gave me a chance to examine "mysql_real_escape_string"
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.