Last active
December 22, 2015 00:09
-
-
Save tamboer/6386926 to your computer and use it in GitHub Desktop.
xml array to xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function createXml($posts, $format = null) | |
| { | |
| /* output in necessary format */ | |
| if ($format == 'json') { | |
| header('Content-type: application/json'); | |
| echo json_encode(array('posts' => $posts)); | |
| } else { | |
| header('Content-type: text/xml'); | |
| echo '<posts>'; | |
| foreach ($posts as $index => $post) { | |
| if (is_array($post)) { | |
| foreach ($post as $key => $value) { | |
| echo '<', $key, '>'; | |
| if (is_array($value)) { | |
| foreach ($value as $tag => $val) { | |
| echo '<', $tag, '>', htmlentities($val), '</', $tag, '>'; | |
| } | |
| } | |
| echo '</', $key, '>'; | |
| } | |
| } | |
| } | |
| echo '</posts>'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment