array()); foreach($files as $file) { $count = count(glob($file."/*")); #add name value pairs to array one at a time in the form "pokemon => 3" $json["categories"][basename($file)] = $count; } header("Content-type: application/json"); print(json_encode($json)); } # outputs a random question about the provided category in XML function outputXml($url, $category) { $files = glob($url . "$category/*"); $index = array_rand($files); // this is a great place to use list!! list($ques, $ans) = file($files[$index]); $dom = new DOMDocument(); $card = $dom->createElement("card"); $dom->appendChild($card); $question = $dom->createElement("question"); $question->appendChild($dom->createTextNode($ques)); $card->appendChild($question); $answer = $dom->createElement("answer"); $answer->appendChild($dom->createTextNode($ans)); $card->appendChild($answer); header("Content-type: text/xml"); print($dom->saveXML()); } ?>