Wrong Display Converting Array to XML Result in PHP -
i have trouble display xml in php. before it, converted array code xml. got array result oci_fetch_array
while ($data = oci_fetch_array($stmt, oci_both)) { $parent = $data['parent']; if($data['parent'] == 0) { $idx++; $idx2 = 0; $product[$idx]['text'] = $data['name']; $product[$idx]['cls'] = "folder"; } else { $product[$idx]['children'][$idx2]['id'] = $data['id']; $product[$idx]['children'][$idx2]['name'] = $data['name']; $product[$idx]['children'][$idx2]['code'] = $data['code']; $idx2++; } }
and array result:
array ( [-1] => array ( [children] => array ( [] => array ( [id] => 4 [name] => speedy 384 kb [code] => speedy_384kb ) [1] => array ( [id] => 7 [name] => speedy 2 mb [code] => speedy_2mb ) ) ) )
then used php code convert xml, code:
$product2=array($product); $xml = new simplexmlelement("<services/>"); array_walk_recursive($product2, array ($xml, 'addchild')); $string=$xml->asxml(); echo '<pre>', htmlentities($string), '</pre>';
yes, got result it's not hope. result...
<?xml version="1.0"?> <services> <4>id</4> <speedy yoo>name</speedy yoo> <speedy_loo>code</speedy_loo> <7>id</7> <2 mb>name</ 2 mb> <u2mb>code</u2mb> </services>
the result want this:
<?xml version="1.0"?> <services> <id>4</id> <name>speedy yoo</name> <code>speedy_loo</code> <id>7</id> <name>2 mb</name > <code>u2mb</code> </services>
how can right one? me, thanks
you need use array_flip() before using simplexmlelement because uses array index values , array values tag names.
presuming $product contains data:
$product2=array(array_flip($product)); $xml = new simplexmlelement("<services/>"); array_walk_recursive($product2, array ($xml, 'addchild')); $string=$xml->asxml(); echo '<pre>', htmlentities($string), '</pre>';
Comments
Post a Comment