我的數據是從客戶的CMS拉昇,但我發現了奇怪的結果PHP array_key_exists返回false錯誤
print_r($appliance_data);
foreach ($appliance_data as $adKey => $adValue) {
print_r($adKey);
print_r($adValue);
print_r(array_key_exists($adKey, $appliance_data));
print_r(isset($appliance_data[$adKey]));
}
隨着輸出
Array
(
[94] => stdClass Object
(
[operation] => 0
[value] => 0
)
[102] => stdClass Object
(
[operation] => 501
[value] => 4
)
[90] => stdClass Object
(
[operation] => 0
[value] => 0
)
)
94
stdClass Object
(
[operation] => 0
[value] => 0
)
(boolean) FALSE
(boolean) FALSE
102
stdClass Object
(
[operation] => 501
[value] => 4
)
(boolean) FALSE
(boolean) FALSE
90
stdClass Object
(
[operation] => 0
[value] => 0
)
(boolean) FALSE
(boolean) FALSE
任何想法是什麼引起的?
編輯:錯誤是array_key_exists AND isset 返回通過循環數組獲得的密鑰FALSE!
serialize($appliance_data)
a:7:{s:2:"94";O:8:"stdClass":3:{s:9:"operation";s:3:"514";s:5:"value";s:1:"2";s:9:"frequency";s:1:"0";}s:3:"102";O:8:"stdClass":3:{s:9:"operation";s:3:"511";s:5:"value";s:1:"4";s:9:"frequency";s:1:"1";}s:2:"90";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}s:2:"68";O:8:"stdClass":3:{s:9:"operation";s:3:"501";s:5:"value";s:1:"3";s:9:"frequency";s:1:"2";}s:2:"66";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}s:2:"84";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}s:2:"98";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}}
一些代碼的爲奇數(將無法運行,因爲它是 - 太多的括號在print_r()之後)。循環位看起來應該運行良好,而最後一行應該會導致錯誤。你可以通過運行確認嗎?當我嘗試類似的代碼時,我的循環運行良好。 – 2012-04-04 09:37:22
@Matt,額外的支架是由於代碼深入客戶CMS內。該數組由客戶端CMS提供,所以我認爲它的編碼不正確! – Zeophlite 2012-04-11 03:32:55