仅使用1个子节点迭代zend_config_xml? [英] Iterating zend_config_xml with only 1 child node?

查看:97
本文介绍了仅使用1个子节点迭代zend_config_xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的zend_config_xml实例,该实例存储一些库存信息,例如哪些产品正在补货(replenish_departments)和哪些产品无法重新订购(fashion_departments). (仅供参考,我们的产品分为部门,每个部门都有唯一的字母代码) 我的xml类似于:

I have a basic zend_config_xml instance that stores some inventory information such as which products are on replenishment (replenish_departments) vs which products can't be reordered (fashion_departments). (fyi our products are classified into departments, each department has a unique alpha code) My xml looks similar to:

<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>

我需要做的是迅速确定给定部门代码是补货还是时尚.我正在尝试的事情很简单(或者我以为如此):

What I need to be able to do is quickly tell if a given department code is in replenish or fashion. What I was trying was simple (or so I thought):

foreach ($inv_settings->replenish_departments as $replenish_deptcode) {
 if ($given_deptcode == $replenish_deptcode) return true;
}

但是,我发现的是,当只有一个子节点时,您无法遍历该子节点.换句话说,此代码字代表fashion_departments,但不是replenish_departments.

However, what I discovered was that when there is a single child node, you cannot iterate through it. In other words, this code words for fashion_departments, but not replenish_departments.

这有什么窍门?

我发现,如果我将$ inv_settings转换为foreach内部的数组,则可以进行迭代而不会出现错误.目前,这是我正在使用的方法,但我仍然可以寻求更好的解决方法.

I've discovered that if I typecast $inv_settings as an array inside of the foreach, I am able to iterate without an error. For now, this is the method I'm using, but I'm still open to a better fix.

推荐答案

仅针对那些最终来到这里的人(例如我).

Just for those other people who end up here (like me).

希望与大家分享,现在可以使用最新版本的zend Framework完成此任务.使用了1.11.11,但已经有一段时间进行了修复,请参见 http://framework.zend.com/issues/browse/ZF-2285

Wanted to share that this can be accomplished now with the latest version of zend framework. Used 1.11.11, but fix has been in awhile see 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"
      }
    }
  }
}

它似乎不允许根多个元素.

It does not appear to allow root multiple elements.

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

这篇关于仅使用1个子节点迭代zend_config_xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆