如何创建一个匿名函数来循环遍历数组,传递参数和返回字符串?

时间:2017-09-04 01:56:17

标签: php anonymous-function

我有以下查询数组:

<?php

$queries_0 = [
                /*
                  Custom Info
                */                
                0=>"INSERT INTO tbl1(field1, field2,field3,created_at) VALUES ('$p[0]', '$p[2]', '$p[2]', 'CURRENT_TIMESTAMP');",
                1=>"SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
                    FROM ((Orders
                    INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
                    INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID)
                    WHERE
                    Shippers.ShipperName='$p[0]'",


                /*
                  Payments
                */
                2=>"SELECT * FROM Customers
                    WHERE City LIKE '$p[0]%';",
                3=>"SELECT * FROM Customers
                    WHERE Country='$p[0]' AND City='$p[1]';",

];

目标是获得所需的查询,仅传递其关键和必要参数。

为实现这一目标,我就是这样做的:

$queries = array( 
  0=>function(array $p) {
    return "INSERT INTO tbl1(field1, field2,field3,created_at) VALUES ('$p[0]', '$p[2]', '$p[2]', 'CURRENT_TIMESTAMP');";
  },

  1=>function(array $p) {
    return "SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
                    FROM ((Orders
                    INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
                    INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID)
                    WHERE
                    Shippers.ShipperName='$p[0]'";
  },

  2=>function(array $p) {
    return "SELECT * FROM Customers
                    WHERE City LIKE '$p[0]%';";
  },

  3=>function(array $p) {
    return "SELECT * FROM Customers
                    WHERE Country='$p[0]' AND City='$p[1]';";
  }
);

测试:

$sql=$queries[0](['f1','f2','f3']);
echo $sql;
$sql=$queries[3](['US','NewYork']);
echo $sql;

结果:

INSERT INTO tbl1(field1, field2,field3,created_at) VALUES ('f1', 'f3', 'f3', 'CURRENT_TIMESTAMP');    
SELECT * FROM Customers WHERE Country='US' AND City='NewYork'; 

现在我想清理该阵列查询,使其像 $ queries_0 一样。这里的目标是避免重复功能,因为我们正在做同样的事情 - 创建一个anonyomous函数并传递参数$ p并返回字符串。

到目前为止,这是我的尝试,没有匿名功能,而且无法正常工作:

function get_sql(array $param) {
    foreach ($param as $k => $p) {
        return $queries_0[$k];//$queries_0[$k] is the string itsef.
    }
}


$res=get_sql(["0"=>['f1','f2','f3']]);
echo $res; //null

有或没有匿名函数的方法应该是什么?

2 个答案:

答案 0 :(得分:0)

我建议你使用PDO和参数绑定。例如,通过使用位置“?”占位符...

$queries_0 = [
    'INSERT INTO tbl1(field1, field2,field3,created_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP)',
    // and so on
];

$pdo->prepare($queries_0[0])->execute(['f1','f2','f3']);

答案 1 :(得分:0)

我同意使用PDO参数绑定 但是如果你想使用自己的图书馆 这是我的解决方案。

    function getSQL(array $params){
    $opts = array(
                    '0'=>'SELECT * FROM tbl WHERE username=?'
                   ,'1'=>'INSERT INTO tbl VALUES(?,?,?,?)'
                   ,'2'=>'UPDATE tbl SET username=? WHERE id=?'
                   ,'3'=>'DELETE FROM tbl WHERE id=?'
                ); //queries

    foreach($params as $param => $sub){
        if(!is_array($sub) && !($sub===null))return null; // invalid -- value not array or null
        $query = $opts[$param];
        if(substr_count($query,'?') != sizeof($sub))return null; // invalid -- not equal number of params
        $last_position = strpos($query,'?');
        foreach($sub as $value){
            $query = substr_replace($query,$value,strpos($query,'?',$last_position),sizeof($query));
            $last_position += strlen($value)+1;
        }
    }
    return $query;
}
echo getSQL(['0'=>['0']]).'<br/>';              // SELECT * FROM tbl WHERE username=0
echo getSQL(['1'=>['0','gello?','gel?g?g?g','3']]).'<br/>'; // INSERT INTO tbl VALUES(0,gello?,gel?g?g?g,3)