tags:

views:

48

answers:

4

Hi all,

I've got a really bad brain block with this.

My XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <fruits>
    <item>Apple</item>
    <item>Orange</item>
    <item>Banana</item>
  </fruits>
  <vegetables>
    <item>Lettuce</item>
    <item>Carrot</item>
  </vegetables>
</data>

I am tyring to use SimpleXML to retrieve an array containing "Apple, Orange, Banana". The code I am using is as follows:

$xml=simplexml_load_file('food.xml');

foreach($xml as $fruits=>$item) {
  $foodlist[] = $item;
}

print_r($foodlist); // Should display list of fruits.

But the list of fruits is not being stored to the array. What am I doing wrong?

Much thanks.

A: 

Try foreach( $xml['data'] as $fruits=>$item )

---- edit ----

foreach( $xml as $fruits => $item ) {
    if( $fruits == "fruits" )
        $foodlist[] = $item;
}
hookedonwinter
That would go through the vegetables too.
ceejayoz
Thanks, but that returns a syntax error. :)
Rudi
@ceejayoz true that. Updated
hookedonwinter
Thanks for that edit, but it still returns a fairly confusing looking array when I print_r (with references to SimpleXML). Please see my comment to egis's answer for an idea of what I need. Apologies for my unclear description in the first place.
Rudi
A: 

Is this it ? http://www.php.net/manual/fr/simplexmlelement.xpath.php

MatTheCat
Looks promising - I'll give it a whirl. Thanks.
Rudi
+1  A: 

I've tested your code. It works fine for me. Another thing - it might be that you described it wrong, or might be that you understand this wrong. $foodlist should contain array of SimpleXML element objects (in your case "<fruits>" and "<vegetables>"), not array of fruits. If you want get only fruits you should access $xml->fruits->item;.

Edit: if you want to build an array of fruits try this:

$array = (array)$xml->fruits;
print_r($array['item']); // Should dipslay list of fruits

//or this
foreach ($xml->fruits->item as $fruit){
  $array2[] = (string) $fruit; //typecast to string, because $fruit is xml element object.
}
print_r($array2); // Should dipslay list of fruits
egis
Thanks for that. I think we're close. I basically want my final array ($foodlist) to be identical to what it would be had I used this command: `$foodlist = array("Apple","Orange","Banana");`. I just want it to be a straight array, devoid of any reference to the originating XML. What is the best way of doing this?
Rudi
You can typecast `$xml->fruits` object to array (`$array = (array) $xml->fruits;`. Your fruits will be in `$array['item']`. Also you can try this `foreach ($xml->fruits->item as $fruit){ $array[] = (string) $fruit; //typecast to string, because $fruit is xml element object.}`Thats the closest thing I can get without researching more into simpleXML. :)
egis
Sorry for bad formatting of code in previous comment.
egis
Wasn't abble to add proper formating to comment, so edited my answer and put there the code.
egis
+1  A: 

How about this:

foreach($xml->fruits->item as $item) {
    //$item has to be cast to a string otherwise it will be a SimpleXML element
    $foodlist[] = (string) $item;
}
print_r($foodlist);

I think this should give you what you're looking for, an array containing the text value of each of the item nodes that are children of the fruits node.

Jeremy
Bingo. Thanks a bunch to everyone. This simple exercise has taught me a lot! :)
Rudi