Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php数组中获取密钥并使用SQL WARE。。在里面子句来筛选结果_Php_Mysql_Arrays - Fatal编程技术网

从php数组中获取密钥并使用SQL WARE。。在里面子句来筛选结果

从php数组中获取密钥并使用SQL WARE。。在里面子句来筛选结果,php,mysql,arrays,Php,Mysql,Arrays,我有以下动态$array1: array(6) { [1]=> string(5) "false" [2]=> string(5) "true" [3]=> string(5) "false" [4]=> string(5) "false" [5]=> string(5) "true" [10]=&g

我有以下动态$array1:

    array(6) { [1]=> string(5) "false" 
               [2]=> string(5) "true" 
               [3]=> string(5) "false" 
               [4]=> string(5) "false" 
               [5]=> string(5) "true" 
               [10]=> string(5) "false" 
             }
  • 我想使用loop创建带有$array1键的$array2,其中value=“true”。 所以最后我有:
  • 然后使用此数组(带bind_param)以“WHERE…IN…”过滤我的结果:
  • 我正在努力解决第一个问题,所以甚至无法检查第二个问题——如果我能将$array2绑定到字符串的话


    希望你的问题很清楚。

    这两部分是你的问题

    要过滤数组,可以使用
    array\u filter()
    ,在使用字符串时,需要检查值是否为“true”以过滤掉“false”值

    $array2 = array_keys(array_filter($array1, function($data) { return $data == "true"; }));
    
    使用
    array\u keys()
    获取索引列表

    下一部分是构建SQL,然后绑定值。您应该为每个值构建一个具有适当数量的
    占位符的查询

    SELECT sapp.appointment_id, sapp.time_start
        FROM ss_appointments sapp 
        WHERE sapp.service_id IN (?,?) AND sapp.time_start >= ?
    
    然后,绑定也将是(例如)

    因此,它动态构建
    iis
    数据类型和要绑定的字段列表。然后,使用参数unpacking(
    )操作符将它们放入

    // Build comma separater list of placeholders
    $in = trim(str_repeat("?,", count($array2)),",");
    // Create appropriate bind data type string
    $type= str_repeat("i", count($array2));
    $sql = "SELECT sapp.appointment_id, sapp.time_start
        FROM ss_appointments sapp 
        WHERE sapp.service_id IN (".$in.") AND sapp.time_start >= ?";
    if ($result = $link->prepare($sql)) {
        // Add the period to the bind data
        $array2[] = $period_from;
        $result->bind_param($type.'s', ...$array2);
        $result->execute();
        $result->bind_result($app_id, $time_start);
        while($result->fetch()){
    
            echo '..results here..';
    
        }
    }
    

    已经有一段时间了,没有经过测试:

    //filter out false, space and 0 and get keys
    $params = array_keys(array_filter($array));
    
    //add other variables in order of ? appearence
    $params[] = $period_from;    
    
    //create a comma separated list of ?
    $list = implode(',', array_fill(0, count($params), '?'));
    
    //use $list in the IN clause
    $result = $link->prepare("SELECT sapp.appointment_id, sapp.time_start
        FROM ss_appointments sapp 
        WHERE sapp.service_id IN ($list) AND sapp.time_start >= ?";
    
    //create types of string for each variable
    $result->bind_param(str_repeat('s', count($params)), $params);
    

    $array2=array\u键(array\u过滤器($array))非常感谢,喜欢你的动态绑定字段列表解决方案!
    
    $result->bind_param('iis', 2, 5, $period_from);
    
    // Build comma separater list of placeholders
    $in = trim(str_repeat("?,", count($array2)),",");
    // Create appropriate bind data type string
    $type= str_repeat("i", count($array2));
    $sql = "SELECT sapp.appointment_id, sapp.time_start
        FROM ss_appointments sapp 
        WHERE sapp.service_id IN (".$in.") AND sapp.time_start >= ?";
    if ($result = $link->prepare($sql)) {
        // Add the period to the bind data
        $array2[] = $period_from;
        $result->bind_param($type.'s', ...$array2);
        $result->execute();
        $result->bind_result($app_id, $time_start);
        while($result->fetch()){
    
            echo '..results here..';
    
        }
    }
    
    //filter out false, space and 0 and get keys
    $params = array_keys(array_filter($array));
    
    //add other variables in order of ? appearence
    $params[] = $period_from;    
    
    //create a comma separated list of ?
    $list = implode(',', array_fill(0, count($params), '?'));
    
    //use $list in the IN clause
    $result = $link->prepare("SELECT sapp.appointment_id, sapp.time_start
        FROM ss_appointments sapp 
        WHERE sapp.service_id IN ($list) AND sapp.time_start >= ?";
    
    //create types of string for each variable
    $result->bind_param(str_repeat('s', count($params)), $params);