2010-08-26 60 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>