takethetrain Posted March 27, 2006 Share Posted March 27, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/5935-array-data-xml-file-no-output/ Share on other sites More sharing options...
takethetrain Posted March 27, 2006 Author Share Posted March 27, 2006 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.. Quote Link to comment https://forums.phpfreaks.com/topic/5935-array-data-xml-file-no-output/#findComment-21264 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.