我有一個基本的zend_config_xml實例,它存儲一些庫存信息,例如哪些產品在補貨(replenish_departments)上,哪些產品不能重新排序(fashion_departments)。 (FYI我們的產品分爲部門,每個部門都有一個獨特的字母代碼) 我的XML看起來類似於:只用1個子節點迭代zend_config_xml?
<inventory>
<settings>
<allow_backorders>1</allow_backorders>
<replenish_departments>
<department>M</department>
</replenish_departments>
<fashion_departments>
<department>MF</department>
<department>MS</department>
</fashion_departments>
</settings>
</inventory>
我需要什麼,能夠做的就是迅速判斷一個給定的部門代碼是在充值或時尚。我是想很簡單(或因此我想):
foreach ($inv_settings->replenish_departments as $replenish_deptcode) {
if ($given_deptcode == $replenish_deptcode) return true;
}
不過,我發現的是,當有一個子節點,你不能遍歷它。換句話說,這個代碼爲fashion_departments而不是replenish_departments。
這裏有什麼竅門?
編輯:我發現,如果我將foreach中的$ inv_settings作爲一個數組進行類型轉換,我可以迭代而不出錯。目前,這是我正在使用的方法,但我仍然樂意提供更好的解決方案。
如果你查詢XML,XPath會不會爲你做得更好? – 2010-08-26 16:21:55
可能,但由於Zend_Config_Xml沒有本地xpath方法來訪問,它可能會在這裏矯枉過正。 – user280725 2010-08-27 19:27:53
你如何將數組$ inv_settings作爲一個數組?我有同樣的問題,並且'foreach((array)$ inv_settings-> replenish_departments爲$ replenish_deptcode)'不解決它。 – Seb33300 2014-06-30 10:13:31