- mt_rand: documentation ( source)
- arsort: documentation ( source)
- range: documentation ( source)
- header: documentation ( source)
<?php
/**
* Created by PhpStorm.
* User: efik
* Date: 21.09.15
* Time: 13:28
*/
$podcasts = [];
foreach (range(1, 150) as $index) {
$podcasts[] =
[
"id" => $index * mt_rand(1, 4),
"title" => "Song - " . $index,
"time" => $index,
"License" => "MIT",
];
}
arsort($podcasts);
$dom = new DOMDocument("1.0", "UTF-8");
$podcastDOM = $dom->createElement("podcast");
foreach ($podcasts as $podcast) {
$item = $dom->createElement('song');
foreach ($podcast as $key => $value) {
if ($key !== "title") {
$currentKey = $dom->createAttribute($key);
$currentKey->appendChild($dom->createTextNode($value));
} else {
$item->appendChild($dom->createTextNode($value));
}
$item->appendChild($currentKey);
$podcastDOM->appendChild($item);
}
}
$dom->appendChild($podcastDOM);
header("Content-Type: text/xml");
echo $dom->saveXML();