我試圖在數組中使用動態對象內的值的值。
在我的具體情況我有一個這樣的數組。
$this->customer = array(
[dealerId] => 4
[billFirstName] => Joe
[billLastName] => Blo
[billAddress1] => 1010s
[billAddress2] => 1020s
[billCity] => Anytown
[billState] => ST
[billCountry] => USA
[billPostalCode] => 11111
[dEmail] => emailaddress
[billPhone] => 8008008888
[password] => password
[distPrice] => 5
[distCost] => 20);
$result = $this->keyCheck('dealerId', 'customer');
我使用的方法:
protected function keyCheck($key, $array, $type = false)
{
if(array_key_exists($key, $this->$array) && $this->$array[$key]):
return $this->$array[$key];
else:
return $type;
endif;
}
首先檢查工作(array_key_exists($鍵,$此 - > $陣列))。但是第二個檢查失敗($ this - > $ array [$ key]),即使在該數組的索引中存在一個值。我已經通過使用print_r($ this - > $ array)證明了該數組存在於keyCheck()方法內部。在方法裏面。我知道我正在尋找的價值可以通過使用,print $ this - > $ array ['dealerId'];
不要被掛了名,或方法我用,就是我中找出如何解決一個數組,它是動態的以這種方式處理保存的值很感興趣。
這可能那麼容易,一旦它發現我會拍着我的頭......
你最好的var_dump if語句,你是可疑內的部分,看看你會得到什麼。 print_r不會信任空值。 – Melsi 2012-03-26 22:18:46
如何使用isset()?它會改變什麼嗎? – 2012-03-26 22:22:18
謝謝,我試過了,數組中沒有空值。的方法中的試驗「打印$此 - > $陣列[‘dealerId’]」產生我在尋找的價值,但解決由於某種原因不能正常工作的變量指標。 – Foaminator 2012-03-26 22:22:41