开发者

php array replace

I have 2 output from a php function as below

First output gives me some thing like this

INSERT INTO "table1" ('column1', 'column2', 'column3') VALUES (?,?,?)
INSERT INTO "table2" ('column1', 'column2', 'column3','column4','column5') VALUES (?,?,?,?,?)

Other output gives

Array = (
1 => 'value1',
2 => 'value2',
3 => 'value3',
)

Array = (
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4',
5 => 'value5',
)

and now I want to do the following.. How can I replace the "?" in the first INSERT QUERY with values value1, value2 and value3 and same way for the second query th开发者_运维知识库e values in the second array.

regards


echo $query1 =str_replace("?,?,?","'".implode("','",$values1 )."'",$query1);
echo $query2 =str_replace("?,?,?","'".implode("','",$values2 )."'",$query2);


Generic solution:

function replaceWithArray($query, $array) {
    $n = 0;
    return preg_replace('~\?~e', '$array[$n++]', $query);
}

example on Codepad

Note: If you want to replace ? only betwen parenthesis, use a lookbehind like this: preg_replace('~\?(?=[^\(\)]*\))~e', '$array[$n++]', $query);


This exact sollution can be done like this:

$query1 = "INSERT INTO `table1` (`column1`, `column2`, `column3`) VALUES (?,?,?)";
$query2 = "INSERT INTO `table2` (`column1`, `column2`, `column3`,`column4`,`column5`) VALUES (?,?,?,?,?)";

$values1 = array("value1","value2","value3");
$values2 = array("value1","value2","value3","value4","value5");


function insertIntoQuery($query, $values){
    $countval = count($values);
    $replace = array();
    for($i=0; $i<$countval; $i++){
        $replace[] = '?';
    }
    $implode = implode(",", $replace);
    $values = implode("','", $values);
    $query = str_replace($implode, "'".$values."'", $query);
    return $query;
}

echo insertIntoQuery($query1, $values1);
echo "<br />";
echo insertIntoQuery($query2, $values2);


If you are using the ?,?,? within the select statement it is meta-syntactic and MySQL will substitute those variables with real values at run time. This can be dangerous if your format is variable, but if you are using static data MySQL's first guess will always be the correct one if it was aligned correctly to begin with.

According to the Php 5.x manual there is a function call for this array_replace, which oddly enough looks a lot like your example. I'm running Php 5.2 and the server has always just balked at that function call like so:

Fatal error: Call to undefined function array_replace()

But both CLI and mod_php are post 5.2xx

[root@networktag dom]# php -i | grep -i version PHP Version => 5.2.10

Now while the example at http://php.net/manual/en/function.array-replace.php says to replace the array this way, until whoever created the programming language interpreter catches up to whoever wrote the manual it doesn't seem to work on a vanilla datacenter installed Linux box running CentOS 5.2 with PhP rpmforge upgrades to 5.2. It really is a moot point since Php 5.x+ support array de-referencing like so:

$array_1 = array('Tom','Dick','Harry');
$array_1[1] = "Car 54";
print_r($array_1);

outputs: Array ( [0] => Tom [1] => Car 54 [2] => Harry )

If you need a more fine grain of control on de-referencing or replacing the array values then don't rely on default indexing in the primary array. Create an incremental counter then cast the array index with it such as $array[$i]=array("Foo" => "Bar", "Bar" => "Foo"); where $i is $i++.

...OR... if you are using a newer version of Php you can even reference the sub index from outside the logical boundary with

print_r($array[$i])(0);

:)


$foo = implode("','",$array);
INSERT INTO "table1" ('column1', 'column2', 'column3') VALUES ('$foo')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜