user1807404
user1807404

Reputation: 17

Putting XML into array then sort?

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"

Upvotes: 0

Views: 185

Answers (6)

thaJeztah
thaJeztah

Reputation: 28997

Try this:

foreach($xml->animal as $child) {
  // cast to integer to prevent SimpleXmlElement being used as key
  $aniID = (int) $child->aniID;
  $animalArray[$aniID] = (string) $child->Name;
}

// sort by value
asort($animalArray);

I assume here that 'aniID' is unique and 'name' may be NOT unique, therefore I chose aniID to be the key of my associative array

then output the results

foreach($animalArray AS $aniId=>$name)
{
   $page.='<option value="'.$aniId.'">'.$name."</option>";
}

[updated] Added casting to prevent SimpleXml objects being used instead of the value

Upvotes: 1

Josef Kufner
Josef Kufner

Reputation: 2989

Little offtopic: Appending to string is pretty slow operation. Using output buffer and echo is much faster. See http://php.net/manual/en/function.ob-start.php

Upvotes: 1

Josef Kufner
Josef Kufner

Reputation: 2989

Take a look at usort function: http://php.net/manual/en/function.usort.php

$xml = simplexml_load_file("animal.xml");

$animals = array();
foreach($xml->animal as $child) {
    $animals[] = array('id' => $child->aniID, 'name' => $child->Name);
}

usort($animals, function($a, $b) { return strcoll($a['name'], $b['name']); });

// Now $animals are sorted by name.

Upvotes: 1

Mathew
Mathew

Reputation: 8279

You aren't assigning the array elements correctly. Try this:

foreach($xml->animal as $child)
 {
   $animalArray[$child->Name] = $child->aniID;
 }

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157967

If you try to add a new index to an accociative array use the following code

foreach($xml->animal as $child)
{
   $animalArray[$child->Name] = $child->aniID;
}

Upvotes: 0

ithcy
ithcy

Reputation: 5589

It's hard to tell what you're expecting, but I think you might be looking for this:

foreach($xml->animal as $child)
{
    $animalArray[$child->Name] = $child->aniID;
}

That is, if you want $animalArray to look like this:

array(
    "Joo" => 1,
    "Moo" => 1,
    "Foo" => 1,
)

Upvotes: 2

Related Questions