how to convert array into variables in php
<p>I want to create a function that takes the name of a table and an array with the column name and value to use it to generate an INSERT in MySQL</p>

<p>Check my code.</p>

<p>Unfortunately this technique does not work as it always generates a comma after each value and this generates an error when entering data. Anyone know a better way to create this function?</p>

<pre><code>&lt;?php
$arrayDados = array('nome' =&gt; 'Renato', 'idade' =&gt; 24, 'peso' =&gt; 36, 'mae' =&gt; 'neide');
$colunas = "";
$valores = "";
$tabela = 'cadastro';

    foreach ($arrayDados as $key =&gt; $value) {
        $colunas .=  $key . ', ';
        $valores .= $value.', ';
    }
$sql = "INSERT INTO $tabela ($colunas) VALUES ($valores)";

echo $sql;`
</code></pre>
