這裏是我的腳本(它的一部分,但它涵蓋了什麼,我想告訴):爲什麼isset()函數不能識別的變量,即使它存在
$data = explode($this->delimeter,$buffer);
var_dump($this->callBack);
var_dump($columns);
foreach ($data as $key=>$val) {
if(isset($columns[$key]) && isset($this->callBack[$columns[$key]])) {
echo "called $key<br/>";
call_user_func(array($this,$this->callBack[$columns[$key]]),trim($val));
}
}
,這裏是它的輸出:
array
'FacilityID' => string 'setFacilityID' (length=13)
'FacilityName' => string 'setFacilityName' (length=15)
array
0 => string 'FacilityID' (length=13)
1 => string 'FacilityName' (length=12)
called 1
我的問題: 爲什麼called 1
只能來,而called 0
不來輸出?我的意思是$this->callBack[$columns[$key]]
對嗎?我錯過了什麼。
從我看到的:isset($columns[$key])
是TRUE
兩者,而isset($this->callBack[$columns[$key]])
的第二個條件對於「FacilityID」爲假?爲什麼? PS:從我看到的所有索引都被定義。
EDIT 2我的腳本功能全面:
function parseFile() {
error_reporting(E_ALL);
$f=fopen($this->file,"r");
if($f) {
$buffer = NULL;
$columns = array();
$data = array();
$firstLine = true;
while(($buffer = fgets($f,4096)) !== false) {
if($firstLine) {
$columns = explode($this->delimeter,$buffer);
foreach ($columns as $key=>$val) {
$columns[$key] = trim($val);
}
$firstLine = false;
}
else {
//not first line
$data = explode($this->delimeter,$buffer);
var_dump($this->callBack);
var_dump($columns);
var_dump($data);
foreach ($data as $key=>$val) {
echo $columns[$key].' ';
if(isset($columns[$key]) && isset($this->callBack[$columns[$key]])) {
echo "called $key<br/>";
call_user_func(array($this,$this->callBack[$columns[$key]]),trim($val));
}
}
}
}
if(!feof($f)) {
throw new Exception("Unexpected error. Parsing stopped in middle");
}
}
else {
throw new Exception("File Not Found");
}
}
我更新的輸出:
array
'FacilityID' => string 'setFacilityID' (length=13)
'FacilityName' => string 'setFacilityName' (length=15)
array
0 => string 'FacilityID' (length=13)
1 => string 'FacilityName' (length=12)
array
0 => string '1' (length=1)
1 => string '"Business Center"' (length=17)
FacilityID FacilityName called 1
你可以在'foreach'之前添加'var_dump($ data);'嗎? –
'var_dump($ data)'在'var_dump($ columns);'後面添加並且輸出已更新 – footy
@footy您是否打開了所有錯誤報告?也許錯誤正在產生... – Endophage