爆炸功能无法正常工作

Explode function is not working properly

我正在从正在运行的数据库中获取输出。下面的数组显示正确的输出。

 $get_elements = array(
        'student_elements' => $row->student_elements, 
        'address_elements' => $row->address_elements,  
        'marketing_elements' => $row->marketing_elements, 
        'office_use_elements' => $row->office_use_elements, 
 );

获取输出

Array
(
    [student_elements] => firstname,lastname,mobileno,age,gender
    [address_elements] => building,sector,city
    [marketing_elements] => 
    [office_use_elements] => counsellername,mobile,email
)

现在我在 explode 函数中传递数组值

$result_elements=explode(',',$get_elements);

获取错误

Severity: Warning
Message:  explode() expects parameter 2 to be string, array given

我想在 foreach

中传递 $result_elements
$results = [];
foreach ($result_elements as $value) {
    echo $sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'";
    $fetch_query = $this->db->query($sql_elements_get);
    foreach ($fetch_query->result() as $r){
        $results[] = $r;
    }
}
return $results;// I

我想 运行 每次查询一次,以获取来自 explode 函数的每个值的输出。

你能帮我解决这个问题吗?

分解函数需要第二个参数作为字符串而不是数组。你必须进一步深入你的数组以获得 string

$get_elements = array(
    'student_elements' => $row->student_elements, 
    'address_elements' => $row->address_elements,  
    'marketing_elements' => $row->marketing_elements, 
    'office_use_elements' => $row->office_use_elements, 
);
for($get_elements as $key=>$value){
    $value = explode(',', $value);
  //run your query here
}

您似乎在尝试获取所有字符串中所有唯一元素的列表。正如其他人提到的,您不能 explode 一个数组。循环遍历的一种方法是 implode 数组在一起,然后 explode 它。

<?php
$get_elements = array (
    'student_elements'     => 'firstname,lastname,mobileno,age,gender',
    'address_elements'     => 'building,sector,city',
    'marketing_elements'     => NULL,
    'office_use_elements'     => 'counsellername,mobile,email'
);

// Combine all strings
$get_elements = implode(',',$get_elements);

// DEBUGGING ONLY
echo $get_elements;

// Split the elements
$get_elements = explode(',', $get_elements);

// DEBUGGING ONLY
echo '<pre>'.var_export($get_elements, TRUE).'</pre>';

// YOUR CODE
$results = [];
foreach ($get_elements as $value) {
    // Ensure it's not empty
    if($value != ''){
        echo $sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'";
        $fetch_query = $this->db->query($sql_elements_get);
        foreach ($fetch_query->result() as $r){
            $results[] = $r;
        }
    }
}
return $results;

?>

A Codepad link 用于代码的输出(删除数据库调用)。

不要运行那么多查询;只有 运行 个。最佳做法是始终尽可能减少查询数量。因为您没有区分结果元素的不同值,所以使用 IN 的一个查询将是有效的。

$get_elements=[
    'student_elements'=>'firstname,lastname,mobileno,age,gender',
    'address_elements'=>'building,sector,city',
    'marketing_elements'=>'',
    'office_use_elements'=>'counsellername,mobile,email'
];

$values="'".str_replace(',',"','",implode(",",array_filter($get_elements,'strlen')))."'";

$fetch_query=$this->db->query("SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name IN ($values)");
foreach ($fetch_query->result() as $r){
    $results[] = $r;
}

*注意,我在用逗号连接之前过滤掉所有空字符串,然后将所有值用单引号引起来。


为了强调IN在WHERE子句中的重要性,区别如下:

没有 IN(假设您过滤掉空元素),您对数据库进行了 11 次调用:

SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='firstname'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='lastname'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='mobileno'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='age'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='gender'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='building'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='sector'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='city'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='counsellername'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='mobile'
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='email'

使用 IN 您对数据库进行了 1 次调用:

SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name IN ('firstname','lastname','mobileno','age','gender','building','sector','city','counsellername','mobile','email')