Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/5935-array-data-xml-file-no-output/
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..
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.