I have a string that contain a mysql query:
$string = "UPDATE [table1] SET value = 1 WHERE id = 1";
I would like to change [table1] with the value of this array:
$table['table1']='pages';
$table['table2']='products';
etc
The result should be:
mysql_query( $string ); // "UPDATE pages SET value = 1 WHERE id = 1"
I already tried the following but nothing:
mysql_query( str_replace( '[', '$table[', $string ));
mysql_query( str_replace( array_keys( "[$table]" ), array_values( $table ), $string ));
mysql_query( str_replace( array_keys( "[".$table."]" ), array_values( $table ), $string ));
EDIT This way works, but there is no a easiest way?
str_replace( explode( ',', ( '['.implode( '],[',array_keys( $table ) ) .']' ) ), $table, $string );