简体   繁体   English

从字符串创建逗号分隔的参数

[英]create comma-separated arguments from string

I'm trying to write a PHP function that will generate dynamic MySQL queries along the following lines: 我正在尝试编写一个PHP函数,它将根据以下几行生成动态MySQL查询:

sprintf("INSERT INTO %s VALUES ($arbitrary_length_type_list)", $arbitrary_length_argument_list);

I've succeeded in writing a short routine that will generate my $arbitrary_length_type_list. 我已经成功编写了一个简短的例程,该例程将生成我的$ arbitrary_length_type_list。 It looks something like this: 看起来像这样:

$type_list = '';
$db_map = array('table_1' => array('record_id', 'foreign_key_id', 'string_value_1', 'string_value_2'), 
               'table_2' = array('record_id', 'value_1', 'value_2'));
$table = 'table_1';
foreach($db_map[$table] as $arg){
    if(substr_compare($arg, '_id', -3, 3) == 0){
        $type_list.= ', %d';
    }else{
        $type_list.= ', \'%s\'';
    }
}
sprintf("INSERT INTO %s VALUES ($type_list)", $argument_list);

My difficulty is with the $argument_list for sprintf() (referred to as $arbitrary_length_argument_list in my first code sample), which cannot be a single string or array, but must be individual comma-separated variables or values. 我的困难在于sprintf()的$ argument_list(在我的第一个代码示例中称为$ arbitrary_length_argument_list),它不能是单个字符串或数组,而必须是单个逗号分隔的变量或值。 Is there a way to take an array or string and format it into a comma-separated list of variables or values that will be accepted by sprintf()? 有没有办法采用数组或字符串并将其格式化为以逗号分隔的变量或值列表,这些列表将由sprintf()接受?

To clarify, here's what I want the above routine to output: 为了澄清,这是我希望上述例程输出的内容:

sprintf("INSERT into %s VALUES (%d, %d, '%s', '%s')", $table, $record_id, $foreign_key_id, $string_value_1, $string_value_2);

Or, alternatively, instead of variable names, the actual values, like so: 或者,也可以使用实际值代替变量名,如下所示:

sprintf("INSERT into %s VALUES (%d, %d, '%s', '%s')", 'table_1', NULL, 1, 'string_value_1', 'string_value_2');

I can't use implode() to create the list of arguments ('table_1', NULL, 1, 'string_value_1', 'string_value_2'), because that function generates a single string and sprintf() won't accept the following: 我不能使用implode()创建参数列表('table_1',NULL,1,'string_value_1','string_value_2'),因为该函数生成单个字符串,并且sprintf()不会接受以下内容:

sprintf("INSERT into %s VALUES (%d, %d, '%s', '%s')", 'table_1, NULL, 1, string_value_1, string_value_2');

[note different positions of the quotes] [注意引号的不同位置]

or this: 或这个:

sprintf("INSERT into %s VALUES (%d, %d, '%s', '%s')", array('table_1', NULL, 1, 'string_value_1', 'string_value_2'));

In both cases it gives me an "incorrect number of arguments" error. 在这两种情况下,它都会给我一个“参数数量错误”的错误。

This also doesn't work: 这也行不通:

$args = $db_map['table_1'];
sprintf("INSERT into %s VALUES (%d, %d, '%s', '%s')", function(){foreach($args as $k => $v){${$k} = $v;}});

In the last case the error is "closure could not be converted to string". 在最后一种情况下,错误是“无法将闭包转换为字符串”。

Maybe someone more experienced than myself has a solution? 也许比我更有经验的人有解决方案?

YES

If $value is array 如果$ value是数组

implode( ',', $value);

If $value is string 如果$ value是字符串

str_replace(' ', ',', $value  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM