PHP array into one string

I have one array that is filled by mysql_query. Values that are in this array I want to put into one continuous string for example:

$array = array(1, 2, 3, 4, 5);

INSERT INTO 'new_slovmas'.'$tabulka'('$array[0]', '$array[1]', '$array[3]') 
       VALUES ('$r_1', '$r_2', '$r_3')";

Sense in one table is over 50 columns I do not want to fill it manually, but by WHILE or FOR. So I was thinking about putting values into one string like this:

$array = array("'1',", "'2',", "'3',", "'4',", "'5',");

so now I have this:

echo $array[2]; => '3',

By the cycle I wont to achieve having multiple entries in one variable.

$str = $array[0], $array[1] ... $array[xy];

INSERT INTO 'new_slovmas'.'$tabulka'('$str') 
       VALUES ('$r_1', '$r_2', '$r_3')";

Answers


Use implode()

$string = implode(',', array(1, 2, 3, 4, 5));  // $string = 1,2,3,4,5

PHP's implode function will unpack an array into a string for use in Javascript. Try this:

$str = implode(',',$array);

Use:

join(",", $array);

Also, be sure to escape your values before you insert them into the database. You do this in case that one of your values has a single quote (') or something worse in it (like valid SQL), which would crash your query. You can use mysql_real_escape_string(...) or mysqli_real_escape_string(...) for this


Need Your Help

Problem simulating a key press on Python for Symbian platform on a Nokia 5th ed phone

python symbian nokia pys60

I am developing an app for Nokia 5800 Music Express (S60 5th edition) using PyS60 (Python for S60) ,I want to simulate a KeyPress say if a message comes,I detect a message and Press a Key.

Display section data in UITableView

iphone xcode uitableview

I am new to sectioned UITableVIew.My Problem is I have an array called stringsArray which is having the strings like