0

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"

4
  • FYI: You're populating the array with name as the key and aniID as the value, but you've got them reversed in your foreach. foreach($animalArray as $name => $aniID)... is probably what you want. Commented Jan 30, 2013 at 0:10
  • @user1807404 Please stop changing your code in the question. It invalidates all of the answers people have already provided. Commented Jan 30, 2013 at 0:18
  • I'm sorry. I'll paste it under next time. Commented Jan 30, 2013 at 0:20
  • @user1807404 your last paste makes no sense at all, first of all, you now use $xml->organization (?). Also, you're creating an array of arrays, not an associative array. Also, as ithcy mentions, by editing your question, all answers below are no longer matching your question. Commented Jan 31, 2013 at 21:26

6 Answers 6

2

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,
)
Sign up to request clarification or add additional context in comments.

3 Comments

and what do you see when you print_r($animalArray)?
When I do a print_r, I get Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) [4] => Array ( ) )
Well, you changed the code so your array doesn't fit the way you're trying to access it. Now you've got an array of arrays, but you're treating it as an associative array. $animals[] = array('id' => $child->aniID, 'name' => $child->Name); means that foreach ($animals as $key => $value) won't work anymore. You'd have to foreach($animals as $animal) and then use $animal['id'] and $animal['name'] inside the foreach loop instead.
1

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

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

Comments

1

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.

2 Comments

This seems to be getting me closer to where I am now, please look at my update and tell me why my select list has nothing but "array" in it
Becouse it is not key => value, but array of arrays. $name contain array, so you want: $page.='<option value="'.$name['id'].'">'.$name['name']."</option>";
1

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

Comments

1

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

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.