So let's say I have an XML file like this
<allAnimals>
<animal>
<aniID>1</aniID>
<name>Joo</name>
</animal>
<animal>
<aniID>2</aniID>
<name>Moo</name>
</animal>
<animal>
<aniID>1</aniID>
<name>Foo</name>
</animal>
</allAnimals>
I need to make this into a drop down list where the displayed value is the animal names but in alphabetical order, and the values associated with that displayed value is the animal ID.
In my mind I should be able to do something like:
<?php
$xml=simplexml_load_file("animal.xml")
$animalArray = array();
foreach($xml->animal as $child)
{
$animalArray[$child->name]= $child->aniID;
}
ksort($animalArray);
$page.="<Select>";
foreach($animalArray AS $key=>$value)
{
$page.='<option value="'.$value.'">'.$key."</option>";
}
return $page;
?>
UPDATE
So this is what I have now
$xml = simplexml_load_file("animal.xml");
$animals = array();
foreach($xml->organization as $child) {
$animals[] = array('id' => $child->aniID, 'name' => $child->Name);
}
$page.="<select>";
foreach($animals AS $aniId=>$name)
{
$page.='<option value="'.$aniId.'">'.$name."</option>";
}
$page.="</select>";
Let's not worry about sorting right now because now all I get in the select list is "array" "array" "array"
nameas the key andaniIDas the value, but you've got them reversed in your foreach.foreach($animalArray as $name => $aniID)...is probably what you want.