php - Why are empty arrays being hidden when outputting them as XML? -
i'm trying have php convert nested arrays xml document. i've been unable troubleshoot why arrays no children being hidden xml output entirely; want them show having no children in xml too.
here's relevant code snippet:
private function response_xml_encode($node, $data) { if ($node == null) return false; if (!is_array($data)) return false; foreach ($data $key => $value) { if (is_array($value) && !$this->response_xml_encode_assocarray($value)) { foreach ($value $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 know if array has associative key value pairs, or if has numeric key value pairs // returns true if array has non-numeric key in foreach ($arr $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); }
i call using:
$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();
note $_get
, $_post
empty arrays.
the following gets returned:
<?xml version="1.0" encoding="utf-8"?> <response> <result>0</result> <message>success.</message> <function>test</function> <data> <foo> <bar> <bad> <wolf> <some_value>testtesttest</some_value> </wolf> </bad> </bar> </foo> </data> </response>
but what's expected show <get/>
, <post/>
before shows <foo>
:
<?xml version="1.0" encoding="utf-8"?> <response> <result>0</result> <message>success.</message> <function>test</function> <data> <get/> <post/> <foo> <bar> <bad> <wolf> <some_value>testtesttest</some_value> </wolf> </bad> </bar> </foo> </data> </response>
you have put below code snippet @ starting foreach
loop
foreach ($data $key => $value) { if(array_key_exists($key, $data) && empty($value)) { $node->addchild($key, ""); } //your other code goes here }
above code output desired output below.
<?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>
i had taken code sample , executed creating class. should working also.
Comments
Post a Comment