Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅使用1个子节点迭代zend_config_xml?_Php_Xml_Zend Config Xml - Fatal编程技术网

Php 仅使用1个子节点迭代zend_config_xml?

Php 仅使用1个子节点迭代zend_config_xml?,php,xml,zend-config-xml,Php,Xml,Zend Config Xml,我有一个基本的zend_config_xml实例,它存储一些库存信息,例如哪些产品正在补货(补货部门)哪些产品不能重新排序(时尚部门)。(仅供参考,我们的产品分为多个部门,每个部门都有一个唯一的alpha代码) 我的xml看起来类似于: <inventory> <settings> <allow_backorders>1</allow_backorders> <replenish_departments> <dep

我有一个基本的zend_config_xml实例,它存储一些库存信息,例如哪些产品正在补货(补货部门)哪些产品不能重新排序(时尚部门)。(仅供参考,我们的产品分为多个部门,每个部门都有一个唯一的alpha代码) 我的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>
然而,我发现,当存在单个子节点时,您无法对其进行迭代。换言之,此代码用于时尚部门,但不用于补充部门

这里有什么诀窍


EDIT:我发现,如果我在foreach中键入$inv\u设置作为数组,我就能够无误地进行迭代。目前,这是我正在使用的方法,但我仍然有一个更好的解决方案。

我刚刚很快写了这篇文章,这在你的情况下会起作用,还是这不是你想要的

$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;
    }   
}
$xml=simplexml\u load\u字符串(“
1.
M
MF
太太
");
foreach($xml->settings->fully\u departments作为$fully\u departments){
foreach($department作为$department补充_部门)
{
如果($给定部门代码==$部门)
返回true;
}   
}

您的示例XML配置文件和代码对我来说似乎运行良好。以下是我使用的代码片段:

$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;
    }
}
它给出了预期的输出:

M需要补充

我不确定您是如何得出不能迭代一项的结论的


另外,您可以使用
toArray()
方法以数组形式获取配置(或其中的一部分),而不是对数组进行类型转换。

仅适用于那些最终出现在这里的其他人(如我)

希望与大家分享,现在可以通过最新版本的zend framework实现这一点。使用1.11.11,但修复已过一段时间,请参见

它似乎不允许根元素包含多个元素

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

价值1
价值2

如果您查询XML,XPath会不会对您有更好的帮助?可能,但由于Zend_Config_XML没有本机XPath方法可访问,因此在这里可能有点过火。如何将$inv_设置键入数组?我也有同样的问题,
foreach((数组)$inv\u settings->fully\u deptcode作为$fully\u deptcode)并没有解决它。是的,这就是我想要的,但是Zend\u Config\u Xml没有扩展SimpleXml,所以它的行为有点不同。如果我使用相同的代码,但将$xml切换到Zend_Config_xml实例,我会收到以下错误:“警告:为foreach()提供的参数无效”,而Refully_departments下只有一个department节点。您的代码假定x_departments下有一个department节点。更改代码以查看时尚部门,或在“补充”部门下添加另一个部门,则代码会中断。
    $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"
      }
    }
  }
}
   <inventory>
     value1
   </inventory>
   <inventory>
     value2
   </inventory>