views:

24

answers:

2

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

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.

A: 

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;
    }   
}
jakenoble
Yes, that is what I'm looking for, BUT, Zend_Config_Xml does not extend SimpleXml so it behaves a little differently. If I use that same code, but switch out $xml for a Zend_Config_Xml instance, I receive the following error: "Warning: Invalid argument supplied for foreach()" when there is only 1 deptartment node under replenish_departments.
A: 

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.

salathe
Your code is assuming a single department node under x_departments. Change your code to look at fashion_departments or add another department under replenish_department and the code breaks.