Iterating zend_config_xml with only 1 child node?
I have a basic zend_config_xml instance that stores some inventory information such as which produ开发者_开发知识库cts 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;
}
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.
What's the trick here?
EDIT: 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.
I just wrote this quickly, this will work in your situation or is this not what your after?
$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;
}
}
Your example XML config file and code appears to work fine for me. Here's the snippet that I used:
$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;
}
}
Which gives the expected output:
M needs replenishing!
I'm not sure how you arrived at the conclusion of not being able to iterate over one item.
P.S. Rather than typecast to an array, you can use the toArray()
method to get the config (or a part of it) in array form.
Just for those other people who end up here (like me).
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());
returns
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>
精彩评论