Php 为什么将空数组作为XML输出时隐藏它们?

Php 为什么将空数组作为XML输出时隐藏它们?,php,arrays,xml,hidden,Php,Arrays,Xml,Hidden,我试图让PHP将嵌套数组转换为XML文档。我无法解决为什么没有子元素的数组被完全隐藏在XML输出之外的问题;我希望他们也能在XML中显示为没有孩子 以下是相关的代码片段: private function response_xml_encode($node, $data) { if ($node == null) return false; if (!is_array($data)) return false; foreach ($data as $key => $value)

我试图让PHP将嵌套数组转换为XML文档。我无法解决为什么没有子元素的数组被完全隐藏在XML输出之外的问题;我希望他们也能在XML中显示为没有孩子

以下是相关的代码片段:

private function response_xml_encode($node, $data)
{
  if ($node == null) return false;
  if (!is_array($data)) return false;
  foreach ($data as $key => $value)
  {
    if (is_array($value) && !$this->response_xml_encode_assocarray($value))
    {
      foreach ($value as $val)
        $node->addChild($key, htmlentities($val));
    }
    else if (is_array($value))
    {
      $this->response_xml_encode($node->addChild($key), $value);
    }
    else if (!$this->response_xml_encode_validkey($key))
    {
      $subnode = $node->addChild($node->getName(), htmlentities($value));
      $subnode->addAttribute('keyinvalid', 'true');
      $subnode->addAttribute('key', htmlentities($key));
    }
    else
    {
      $node->addChild($key, htmlentities($value));
    }
  }
}

private function response_xml_encode_assocarray($arr)
{
  // Let's us know if this array has associative key value pairs, or if it has numeric key value pairs
  // Returns TRUE if this array has any non-numeric key in it
  foreach ($arr as $key => $value)
  {
    if (!is_numeric($key)) return true;
  }
  return false;
}

private function response_xml_encode_validkey($key)
{
  if (strpos('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_', substr($key, 0, 1)) === false) return false;
  return (preg_match('/[^A-Za-z_-]/s', $key) === 0);
}
然后,我使用以下方法将其称为:

$response = array(
  'result' => 0,
  'message' => 'Success.',
  'function' => 'test',
  'data' => array(
    'GET'  => $_GET,
    'POST' => $_POST,
    'foo'  => array(
      'bar' => array(
        'bad' => array(
          'wolf' => array(
            'some_value' => 'testtesttest'
          )
        )
      )
    )
  )
);
$root = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>');
$this->response_xml_encode($root, $response);
return $root->asXML();
但我们期望它在显示
之前显示


0
成功。
测试
测试

您必须在
foreach
循环的开始处放入下面的代码片段

foreach ($data as $key => $value)
{    
   if(array_key_exists($key, $data) && empty($value))
   {
       $node->addChild($key, "");
   }
   //your other code goes here
}
上面的代码将输出您想要的输出,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <result>
    </result>
    <message>Success.</message>
    <function>test</function>
    <data>
        <GET>
        </GET>
        <POST>
        </POST>
        <foo>
            <bar>
                <bad>
                    <wolf>
                        <some_value>test test test</some_value>
                    </wolf>
                </bad>
            </bar>
        </foo>
    </data>
</response>

成功。
测试
测试

我将您的代码作为示例,并通过创建一个类来执行它。因此,这也应该对您有效。

这几乎与预期一样有效。我把它改成了if块的else{}部分:else{if(!array_key_exists($key,$data))$node->addChild($key,);else$node->addChild($key,htmlentities($value));}这是我需要的。谢谢注意,我还添加了if(array_key_exists($key,$data)&&is_array($value)&&count($value)==0)$node->addChild($key);到第一个foreach()循环的开头。
foreach ($data as $key => $value)
{    
   if(array_key_exists($key, $data) && empty($value))
   {
       $node->addChild($key, "");
   }
   //your other code goes here
}
<?xml version="1.0" encoding="UTF-8"?>
<response>
    <result>
    </result>
    <message>Success.</message>
    <function>test</function>
    <data>
        <GET>
        </GET>
        <POST>
        </POST>
        <foo>
            <bar>
                <bad>
                    <wolf>
                        <some_value>test test test</some_value>
                    </wolf>
                </bad>
            </bar>
        </foo>
    </data>
</response>