Jump to content

Archived

This topic is now archived and is closed to further replies.

takethetrain

array -> data -> xml file... no output??

Recommended Posts

Hi,

I'm using a function to convert an array to a string (supposedly), and then write that to an external (XML) file. But for some reason, while the string displays correctly within the PHP file, it is not written to the external file. No errors come up.

Original array (shortened for viewing purposes):
[code]
Array
(
    [picture] => Array
        (
            [0] => Array
                (
                    [image] => images/image1.jpg
                    [thumb] => images/th_image1.jpg
                    [title] => Image 1 Title
                    [description] => image 1 description
                )

            [1] => Array
                (
                    [image] => images/th_image2.jpg
                    [thumb] => images/th_image2.jpg
                    [title] => Image 2 Title
                    [description] => image 2 description
                )

        )

)
[/code]


Convert array to XML string:
[code]
function array2xml($array) {
    print "<?xml version='1.0'?>\n\t<gallery>";
    while (list ($num, $tmp) = each ($array)) {
        print "\n\t\t<picture>";
           while (list ($key, $val) = each ($tmp)) {
             print "\n\t\t\t<$key>$val</$key>";
           }
        print "\n\t\t</picture>";
    }
    print "\n</gallery>";
}
[/code]

Displayed in PHP file:
[code]
<?xml version='1.0'?>
    <gallery>
        <picture>
            <image>images/image1.jpg</image>
            <thumb>images/th_image1.jpg</thumb>
            <title>Image 1 Title</title>
            <description>image 1 description</description>
        </picture>
        <picture>
            <image>images/th_image2.jpg</image>
            <thumb>images/th_image2.jpg</thumb>
            <title>Image 2 Title</title>
            <description>image 2 description</description>
        </picture>
</gallery>
[/code]

Write data to a file:
[code]
$xmlstr = array2xml($arr["picture"]);
$fp = fopen ('temp.xml', 'w');  //While the data is not written, the temp file is created successfully.
$write = fputs($fp,$xmlstr);
fclose ($fp);
[/code]

Sorry for the very long post. Thanks for your help!

-Brandy

Share this post


Link to post
Share on other sites
The fix, for anyone who might care:

[code]
function array2xml($array) {

    $output = "";
    if($array) $output .= "<?xml version='1.0'?>\r\n\t<gallery>";

    while (list ($num, $tmp) = each ($array)) {
        $output .= "\r\n\t\t<picture>";

        while (list ($key, $val) = each ($tmp)) {
            $output .= "\r\n\t\t\t<$key>$val</$key>";
            }

        $output .= "\r\n\t\t</picture>";

        }

    if($array) $output .= "\r\n</gallery>";
    return $output;
    }
[/code]

Thanks anyway..

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.