在php中填充空数组或空数组元素

时间:2014-08-26 10:43:58

标签: php arrays file

我在使用这段代码时遇到了困难,我已经将url的代码添加到了 $ query_params数组,如果任何字段中有空值,则填充字符串 '清空',然后保存/追加到文件。问题是表单中是否缺少某些变量 或者有空值,它不会将“空”字符串添加到保存到文件的记录的特定部分 这是代码:

<?php
include 'curr.php';
$url = curPageURL();
$query_str = parse_url($url, PHP_URL_QUERY);
$query = parse_str($query_str, $query_params);
#authenticate the user name and password
if (in_array("password", $query_params) && in_array("usrer1", $query_params)) {
    for( $i=0; $i < 30; $i++ )
        if(empty($query_params[i])) { 
            $query_params[i] = 'Empty'; 
            }
    $file = new SplFileObject("file.csv", "a");
    $written = $file->fputcsv($query_params);
    echo "TRUE";
}
else{
    echo "UNAUTHORIZED";
}
?>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您应该拥有所有可能键的列表,并执行以下操作:

$possible_keys = array('password', 'usrer1', 'c', 'd');
foreach($possible_keys as $key) {
    if (!isset($query_params[$key]) || empty($query_params[$key]))
        $query_params[$key] = 'Empty';
}
相关问题