2010-08-26 59 views
1

我有一個基本的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作爲一個數組進行類型轉換,我可以迭代而不出錯。目前,這是我正在使用的方法,但我仍然樂意提供更好的解決方案。

+0

如果你查詢XML,XPath會不會爲你做得更好? – 2010-08-26 16:21:55

+0

可能,但由於Zend_Config_Xml沒有本地xpath方法來訪問,它可能會在這裏矯枉過正。 – user280725 2010-08-27 19:27:53

+0

你如何將數組$ inv_settings作爲一個數組?我有同樣的問題,並且'foreach((array)$ inv_settings-> replenish_departments爲$ replenish_deptcode)'不解決它。 – Seb33300 2014-06-30 10:13:31

回答

0

我剛寫了這個很快,這會在你的情況下工作,或者這不是你的後?

$xml = simplexml_load_string("<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 ($xml->settings->replenish_departments as $replenish_departments) { 
    foreach ($replenish_departments as $department) 
    { 
     if ($given_deptcode == $department) 
      return true; 
    } 
} 
+0

是的,那就是我正在尋找的,但是,Zend_Config_Xml沒有擴展SimpleXml,所以它的行爲有點不同。如果我使用相同的代碼,但爲Zend_Config_Xml實例切換$ xml,那麼當replenish_departments下只有一個deptartment節點時,會收到以下錯誤:「Warning:爲foreach()提供的無效參數」。 – user280725 2010-08-27 19:27:10

0

您的示例XML配置文件和代碼似乎對我來說工作正常。下面是我使用的代碼片段:我不知道你如何到達不能夠遍歷一個項目結束

M needs replenishing!

$given_deptcode = 'M'; 
$configuration = new Zend_Config_Xml($config_file); 
$inv_settings = $configuration->settings; 
foreach ($inv_settings->replenish_departments as $replenish_deptcode) { 
    if ($replenish_deptcode == $given_deptcode) { 
     echo $replenish_deptcode . ' needs replenishing!' . PHP_EOL; 
    } 
} 

這給預期輸出。

P.S.您可以使用toArray()方法以數組形式獲取配置(或其中的一部分),而不是對數組進行類型轉換。

+0

您的代碼假設x_departments下的單個部門節點。更改您的代碼以查看fashion_departments或在replenish_department下添加其他部門,並停止代碼。 – user280725 2010-08-30 15:49:22

0

只爲那些最終在這裏(像我一樣)的其他人。

想要分享一下,現在可以用最新版本的zend框架來完成這個任務。使用1.11.11,但修復已經在一段時間看到http://framework.zend.com/issues/browse/ZF-2285

$xml = '<?xml version="1.0"?> 
    <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>'; 

    $article = new Zend_Config_Xml($xml); 
Zend_Debug::dump($article->toArray()); 

回報

array(1) { 
    ["settings"] => array(3) { 
    ["allow_backorders"] => string(1) "1" 
     ["replenish_departments"] => array(1) { 
     ["department"] => string(1) "M" 
     } 
     ["fashion_departments"] => array(1) { 
     ["department"] => array(2) { 
     [0] => string(2) "MF" 
     [1] => string(2) "MS" 
     } 
    } 
    } 
} 

它似乎並沒有允許root多個元素。

<inventory> 
    value1 
    </inventory> 
    <inventory> 
    value2 
    </inventory>