Output data created by function -?

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:


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
                )

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

            [3] => Array
                (
                    [image] => images/this.jpg
                    [thumb] => images/th_this.jpg
                    [title] => This is the title.
                    [description] => This is the description.
                )

        )

)

Convert array to XML string:


function array2xml($array) {
    print "<?xml version='1.0'?>
	<gallery>";
    while (list ($num, $tmp) = each ($array)) {
        print "
		<picture>";
           while (list ($key, $val) = each ($tmp)) {
             print "
			<$key>$val</$key>";
           }
        print "
		</picture>";
    }
    print "
</gallery>";
}

Displayed in PHP file:


<?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>
        <picture>
            <image>images/th_image4.jpg</image>
            <thumb>images/th_image4.jpg</thumb>
            <title>Image 4 Title</title>
            <description>image 4 description</description>
        </picture>
        <picture>
            <image>images/this.jpg</image>
            <thumb>images/th_this.jpg</thumb>
            <title>This is the title.</title>
            <description>This is the description.</description>
        </picture>
</gallery>

Write data to a file:


$xmlstr = array2xml($arr["picture"]);
$fp = fopen ('temp.xml', 'w');
$write = fputs($fp,"$xmlstr"); //Quotes or no quotes around the variable, I get the same effect.
fclose ($fp);

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

Hi again–sorry to bump, but this is really driving me nuts. :crying:

Finally resolved this. If you’re curious:


function array2xml($array) {

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

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

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

        $output .= "
		</picture>";

        }

    if($array) $output .= "
</gallery>";
    return $output;
    }

Thanks anyway! :stuck_out_tongue: