Skip to content

Instantly share code, notes, and snippets.

@tamboer
Last active December 22, 2015 00:09
Show Gist options
  • Select an option

  • Save tamboer/6386926 to your computer and use it in GitHub Desktop.

Select an option

Save tamboer/6386926 to your computer and use it in GitHub Desktop.
xml array to xml
<?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