Problem with arrays

Hi fellas,

My Function:

public function Atualiza( $colunas==array() ){

                //get the array keys and turn into string
		$chaves = implode(",",array_keys($colunas));

                //turn the string into array
		$chaves = explode(",", $chaves);

                //get the array values and turn into string
		$valores = implode(",",array_values($colunas));

                //turn the string into array
		$valores = explode(",",$valores);
                $qtd = sizeof($colunas);

			$campo[] = $chaves[$x]."='".$valores[$x]."'";
		$update = implode(", ",$campo);

                $sql = "UPDATE ".$this->nomeClasse." SET ".$update


The function will return a SQL statement like “UPDATE user SET name=‘paul’, email=‘’, password=‘123’, description=‘Aenean suscipit pulvinar felis, vitae luctus nulla viverra quis.’”

The problem is with my array commands and the description field. The description field will receive a text and it may contain commas, what will make my array_values totally useless and return a wrong statement because the array_values will implode in the first comma of the descrition field.

Get it?

Sorry for any typos and any confusion!

And if possible, any ideas to make this function better? :smiley:

Thanks in advance;)

Neither did I javadecaf, thanks for the tip, I didn’t know the power of foreach.

All that code was replaced by

foreach($colunas as $key => $value ){
     $campos[] = $key."='".$value."'";


Thanks =)
        //get the array values and turn into string 
        $valores = implode(",",array_values($colunas)); 

Do I understand correctly that this is where your script is getting derailed? If so, why can’t you just use a different delimiter, some character that wouldn’t be likely to occur, such as ‘|’? This would look like:

       //get the array values and turn into string 
        $valores = implode("|",array_values($colunas)); 

                //turn the string into array 
        $valores = explode("|",$valores); 

Although I think this fix might work, I have to say I really don’t understand why you’re imploding and then exploding, rather than simply using a for loop to copy the values and keys from $colunas into your new arrays, or even using something like:

foreach($colunas as $chave => $valor) {
$campo[] = $chave."='".$valor"'";


No problem - glad I could help. Foreach is one of my favorite functions in PHP, and I always miss it when I’m coding JavaScript.