簡體   English   中英

如何從 3d 數組中獲取一些值到 while 循環內的新數組中

[英]How to get some values from a 3d array into a new array inside a while loop

我想得到一個 CSV 導出工作,用戶選擇一些可選字段以導出必填字段。 我無法將要導出的字段列表添加到此 csv 導出方法中。

我想成為導出一部分的字段列表最終在 $fields 數組中,但我不確定如何將其集成到每一行的導出循環中。

// database conection stuff and $_POST data validation/sanitation above here, not relevant. From a CSV export form.
$exportToCSVQuery = " SELECT $allRequestedFields' FROM my_table ";

// if the (run query) is valid then...                                                                                                                                                              
    if ($exportToCSVResult = mysqli_query($mysqli, $exportToCSVQuery))  {
    // count number of rows in result.
    $exportToCSVResultNumRows = mysqli_num_rows($exportToCSVResult);
// if there is at least one result.
if ($exportToCSVResultNumRows > 0 ) {
    if ($safeDelimiter == 'Comma')  {
        $delimiter = ",";
    }

   elseif ($safeDelimiter == 'Semi-colon')  {
    $delimiter = ";";
    }
    else{
       $delimiter = ",";
        }
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('ID', 'Name', 'em', 'type', 'reason', 'result', 'status', 'notes', 'datetime', 'requestedby', 'requestedbycitrixid', 'week', 'period', 'year', 'historylog');

// put required and optional fields together from the form.
$fields = $fields+$safeFieldsArray;
fputcsv($f, $fields, $delimiter);
//create $linedata information, formatted in a php assoc row format.
//flip the $fields array so the values become the keys, needed for the next step.
$fieldsFlipped = array_flip($fields);
//output each row of the data, format line as csv and write to file pointer
while($exporttocsvrow = $exporttocsvresult->fetch_assoc()){
$fieldsarrayforlinedata = array_intersect_key($exporttocsvrow,$fieldsFlipped);
// flip array
$flippedfieldsarrayforlinedata = array_flip($fieldsarrayforlinedata);
fputcsv($f, $flippedfieldsarrayforlinedata, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $safeFilename . '";');
//output all remaining data on a file pointer
fpassthru($f);
}
else    {
Echo 'No Data to Export';
}
}
else    {
$exportToCSVResultErr = "Error: " .mysqli_error($mysqli) ;
trigger_error("Query Error: ".$exportToCSVQuery." Error:" $exportToCSVResultErr, E_USER_WARNING);
        }

編輯:我嘗試根據以下 Pradeep 的回答添加一些內容到循環中,如下所示:

while($exporttocsvrow = $exporttocsvresult->fetch_assoc()){



$fieldsarrayforlinedata = array_intersect_key($exportToCSVRow,$fieldsFlipped);

unset($valuesArray);
$valuesArray = array();
foreach ($fieldsFlipped as $key){
foreach ($exportToCSVRow[$key] as $values)  {
        $valuesArray[] = $values;
                                                                            }
                                                    }

fputcsv($f, $valuesArray, $delimiter);
}

但這只會導致 CSV 帶有標題和空白數據。 我想我可能錯誤地添加了那個循環。

我希望這將有助於獲得所需的 output。

   $row=array('fieldA'=>array('data1','data2','data2','data3'),'fieldB'=>array('data1','data2','data3'),'fieldC'=>array('data1','data2','data3'));

   $key=array('fieldA','fieldB');

   foreach($key as $key){
        foreach($row[$key] as $values){
        echo $values; 
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM