[英]how to check multiple $_POST variable for existence using isset()?
[英]check for variable existence with isset?
好的,這很奇怪,我反復嘗試使用不同的方法,但沒什么似乎可以滿足我的需要。
我有一個description.txt
文件,其內容格式為:
filename|title|description
現在,對於該文件的每一行,我都會:
$parts = explode('|', $line);
$this->info[$parts[0]]['title'] = isset($parts[1]) ? $parts[1] : 'no-title';
$this->info[$parts[0]]['description'] = isset($parts[2]) ? $parts[2] : 'no-description';
但是,因為我description.txt
文件中的某些行沒有標題和描述,而只有文件名,所以我認為檢查那些缺失部分的存在是可以的,在這種情況下,分別回顯no-title
和no-description
。 但是上面的代碼不會產生這些字符串,並且我看不到任何回聲。
我也嘗試了array_key_exists('1', $parts)
,但沒有成功。 嘗試使用is_null
失敗。 我沒有任何想法為什么這不起作用。
只需添加一下,如果沒有任何由管道分隔的部分丟失,代碼就可以正常工作,所以這只是如何捕獲不存在的部分的問題。
這樣做var_dump
在$parts[1]
對於沒有文件名之后的任何行給我null
這是var_dump($parts); die;
輸出var_dump($parts); die;
var_dump($parts); die;
如果第一行僅是文件名:
array (size=1)
0 => string '10172608_1425353644382351_9063428062883749844_n.jpg
' (length=52)
例如在第一行:
filename
filename|title|description
我設法使其與!empty($parts[1])
檢查一起使用,但是隨后我必須在description.txt
文件中使用它:
filename|||
為了它能正常工作,當我沒有標題或描述時,我想跳過那些多余的管道。
這是整個循環:如果description.txt
文件類似於:
filename|||
用於不包含標題或描述的行。
if($handle = @fopen($this->path . "/" . $this->desc_file, "r")) {
while (($line = fgets($handle)) !== false) {
// process each line and put title & desc into an info array
$parts = explode('|', $line);
$this->info[$parts[0]]['title'] = ! empty($parts[1]) ? $parts[1] : 'no-title';
$this->info[$parts[0]]['description'] = ! empty($parts[2]) ? $parts[2] : 'no-description';
}
}
您需要修剪從文件中讀取的行,因為它們上面有換行符。
我還將所有詳細信息存儲在“信息”數組中...
if ($handle = fopen(__DIR__ ."/description.txt", "r")) {
while (($line = fgets($handle)) !== false) {
$line = trim($line);
// process each line and put title & desc into an info array
$parts = explode('|', $line);
$fileDetails = array();
$fileDetails['filename'] = ! empty($parts[0]) ? $parts[0] : 'no-filename';
$fileDetails['title'] = ! empty($parts[1]) ? $parts[1] : 'no-title';
$fileDetails['description'] = ! empty($parts[2]) ? $parts[2] : 'no-description';
$this->info[] = $fileDetails;
}
fclose($handle);
檢查-有幫助
$parts = explode('|', $line);
$no_title = 'no-title';
if($parts[1]){
$no_title = $parts[1];
}
$no_description = 'no-description';
if($parts[2]){
$no_description = $parts[2];
}
$this->info[$parts[0]]['title'] = $no_title;
$this->info[$parts[0]]['description'] = $no_description;
例如
$line = 'filename';
$no_title = 'no-title';
if($parts[1]){
$no_title = $parts[1];
}
$no_description = 'no-description';
if($parts[2]){
$no_description = $parts[2];
}
echo $no_title .' ';
echo $no_description;
輸出:
no-title no-description
請查看數組function.count的大小
$parts = explode('|', $line);
$this->info[$parts[0]]['title'] = count($parts)==1 : 'no-title';
$this->info[$parts[0]]['description'] = count($parts)==2 : 'no-description';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.