Hi.

I have a strange problem with Zend_Config_Xml.

Here is an example.

With this xml file https://gist.github.com/883465

this code:

$config = new Zend_Config_Xml('config.xml');
var_dump($config->get('elements')->get('element')->toArray());

gives:

array(2) {
  [0]=>
  array(2) {
    ["a"]=>
    array(1) {
      ["attr"]=>
      string(2) "at"
    }
    ["e"]=>
    array(3) {
      [0]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
      [1]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
      [2]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
    }
  }
  [1]=>
  array(2) {
    ["a"]=>
    array(1) {
      ["attr"]=>
      string(2) "at"
    }
    ["e"]=>
    array(3) {
      [0]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
      [1]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
      [2]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
    }
  }
}

with this xml file https://gist.github.com/883469

it gives:

array(2) {
  ["a"]=>
  array(1) {
    ["attr"]=>
    string(2) "at"
  }
  ["e"]=>
  array(3) {
    [0]=>
    array(1) {
      ["attr"]=>
      string(2) "at"
    }
    [1]=>
    array(1) {
      ["attr"]=>
      string(2) "at"
    }
    [2]=>
    array(1) {
      ["attr"]=>
      string(2) "at"
    }
  }
}

and I expect:

array(1) {
  [0]=>
  array(2) {
    ["a"]=>
    array(1) {
      ["attr"]=>
      string(2) "at"
    }
    ["e"]=>
    array(3) {
      [0]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
      [1]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
      [2]=>
      array(1) {
        ["attr"]=>
        string(2) "at"
      }
    }
  }
}

This is tricky when you want to iterate over elements

$config = new Zend_Config_Xml('config.xml');
foreach($config->get('elements')->get('element') as $element);

which is fine if there are more then one elements, but if you have only one,
you'll end up iterating over element children!

Any idea?


---
Regards,
Saša Stamenković

Reply via email to