我有以下陣列訪問PHP 7對象屬性
$arrdata = array("CTypeID","TypeName","CTYPES","CID","COURSES","CTypeID");
此陣列經受功能:Generic2DataSideBar($arrdata);
被調用的函數的定義如下:
function Generic2DataSideBar($data)
{
//a generic array for data..
//var_dump($data);
$sdata= array();
//this is the function for generating depended data...
$this->load->model("Commons");
$dat="SELECT ".$data[0].",".$data[1]." FROM ".$data[2];
$result=$this->Commons->LoadResultSet($dat);
//Looping through the resultset
foreach ($result as $key) {
//get the number of departments
$da="SELECT ".$data[3]." FROM ".$data[4]." WHERE ".$data[5]."='".$key->$data[0]."'";
$dat=count($this->Commons->LoadResultSet($da));
$r=array($key->$data[1], $dat);
array_push($sdata, $r);
}
header("content-type: application/json");
$sdata=json_encode($sdata);
return $sdata;
}
此功能在PHP 5.6.3中運行良好,但是當我升級到PHP 7時,行$da="SELECT ".$data[3]." FROM ".$data[4]." WHERE ".$data[5]."='".$key->$data[0]."'";
帶來了一個問題,$key->$data[0]
返回''
除了該部分外,所有值都正常。在PHP 7中不能有像$key->"Data"
這樣的東西,只要「數據」是一個有效的屬性名稱?謝謝
嘗試使用'($鍵 - > $數據)[0]'和檢查向後不兼容於變量處理與http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling – jitendrapurohit
謝謝,但這是行不通的。產生相同的結果 – NetizenKing