检查while循环键是否存在于单独的数组中


Checking while loop key exists in seperate array

检查了各种问题,但无法找到我所需要的,我有点不知所措。

我试图选择从MySQL的列,我想通过解析列名和添加有效的列名到$colnames数组导出到CSV,然后将这些值作为标题添加到CSV,然后只显示从数据库通过while循环的相关数据。

我特别关注了以下问题,并从其他问题中得到了指导:如何在php中获得多维数组中的所有键

代码如下:

function query_to_csv($query, $filename, $attachment = false, $headers = true, $specs_off = false) {
    if($attachment) {
        // send response headers to the browser
        header( 'Content-Type: text/csv; charset=UTF-8' );
        header( 'Content-Disposition: attachment;filename='.$filename);
        $fp = fopen('php://output', 'w');
    } else {
        $fp = fopen($filename, 'w');
    }
    $result = mysql_query($query) or die( mysql_error() );
    if($headers) {
        // output header row (if at least one row exists)
        $row = mysql_fetch_assoc($result);
        if($row) {
            // PRODUCTS TABLE SPECIFIC - get rid of specs_ and free_ columns so have nicer data set for user
            if($specs_off) { 
                $columnames = array_keys($row);   
                $colnames = array(); 
                //$colnames = array_keys($row);
                foreach($columnames as $key => $value) {
                    if((substr_count($value, "spec_") < 1) && (substr_count($value, "free_") < 1))  {
                        array_push($colnames, $value);                                  
                    }
                }
            }
            else {
                $colnames = array_keys($row);
            }
            // add in additional columns if exporting client data  
            if($table == 'clients') {array_push($colnames, "products", "last_order_date");}
            //write the colnames to the csv file
            fputcsv($fp, $colnames);
            // reset pointer back to beginning
            mysql_data_seek($result, 0);
        }
    } // done with the headers etc, now lets get on with the data

    // clear out and create the $row_data array 
    $row_data = array(); 
    // run through the row array adding values to row_data as we go 
    while($row = mysql_fetch_assoc($result)) {
        // create the array_keys_multi from https://stackoverflow.com/questions/11234852/how-to-get-all-the-key-in-multi-dimensional-array-in-php/11234924#11234924
        function array_keys_multi(array $array) {
            $keys = array();
            foreach ($array as $key => $value) {
                $keys[] = $key;
                if (is_array($array[$key])) {
                    $keys = array_merge($keys, array_keys_multi($array[$key]));
                }
            }
            return $keys;
        }
        // run the function on the $row array
        array_keys_multi($row); 
        // now use the $keys array  
        foreach($keys as $key => $value) {
             // check if the value is in the colnames array and if so push the data on to the $row_data array ready for export to CSV
             if(in_array($value, $colnames)) {
                 array_push($row_data, $row[$value]);
             }
        }
        // now we are ready to write the CSV
        fputcsv($fp, $row_data);
    }
    fclose($fp);
    exit;
} // end of query_to_csv
// Write the sql statement
$sql = "SELECT * FROM ".$table." "; 
if(isset($where_1_col)) { $sql .= " WHERE `".$where_1_col."` = '".$where_1_val."'"; }
if(isset($where_2_col)) { $sql .= " AND `".$where_2_col."` = '".$where_2_val."'"; }
if(isset($where_3_col)) { $sql .= " AND `".$where_3_col."` = '".$where_3_val."'"; }
if(isset($where_4_col)) { $sql .= " AND `".$where_4_col."` = '".$where_4_val."'"; }
if(isset($order_by_col)) { $sql .= " ORDER BY `".$order_by_col."` ". strtoupper($order_by_dir) ." "; }
// output as an attachment
query_to_csv($sql, $table."_export.csv", true, true, true);

所有我得到的是一个巨大的导出所选择的列名重复多少次,从初始查询有值。我不知道怎么把值写进去。

欢迎任何建议,我哪里做错了,或者我如何才能更利落地进行。

似乎您只是将新行数据附加到$row_data,但从未清除该数组。

array_push($row_data, $row[$value]);

我所做的修复:

移动
// clear out and create the $row_data array 
$row_data = array(); 

进入 while循环。

改变
// clear out and create the $row_data array 
$row_data = array(); 
while($row = mysql_fetch_assoc($result)) {
...
}

while($row = mysql_fetch_assoc($result)) {
// clear out and create the $row_data array 
$row_data = array(); 
...
}

注意:
你到处都在使用$table,但从来没有定义过它。像这里的if($table == 'clients')
如果它是一个全局变量,您还需要为函数添加global $table或参数。

编辑:
正如我在对你的问题的评论中提到的,你可以使用array_keys()来获得密钥。php.net/manual/de/function.array-keys.php
然后修改

array_keys_multi($row); 

$keys =  array_keys($row);

之后可以删除array_keys_multi()

进一步,您可以将该部分移动到while循环的前面,因为您只需要计算一次所需的列名,而不是在每次迭代中。