0

I have trouble to display xml in php. before it, i converted an array code to xml. i got array result from 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 this is the 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 i used php code to convert it into xml, this is the code:

$product2=array($product);
$xml = new SimpleXMLElement("<services/>");
array_walk_recursive($product2, array ($xml, 'addChild'));
$string=$xml->asXML();
echo '<pre>', htmlentities($string), '</pre>';

yes, i got result but it's not what i hope. this is the 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 that i want is like 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 i get the right one? help me, thanks

1 Answer 1

1

You need to use array_flip() before using SimpleXMLElement because it uses the array index as values and the array values as tag names.

presuming that $product contains your 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>';
Sign up to request clarification or add additional context in comments.

7 Comments

could you give me an example? @DanieleD ? Cause i try with '$product2=array($product); $products=array_flip($product2); $xml = new SimpleXMLElement("<services/>"); array_walk_recursive($products, array ($xml, 'addChild')); $string=$xml->asXML(); echo '<pre>', htmlentities($string), '</pre>';'
I added one, presuming that $product is your array.
ok, i've tried, but it show '<?xml version="1.0"?> <services/>' only, there's no data... @DanieleD
Can you please tell me the content of $product and $product2 by print_r() ?
the result of print_r() is the array result.. @DanieleD
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.