Please Help - SimpleXML question
Posted: Fri Nov 18, 2005 2:47 pm
Hello!
I am trying to load Book objects with information from an XML file. I'm having problems loading my authors() array in my books object with Authors from the XML file if there is more than one <Author></Author> within the <Book> node. I seem to be stupid when it comes to Arrays! I'm having trouble in my foreach loop for Authors. Currently, I cannot even write the author name into my $authors array I created here, it gives me a "Can't use function return value in write context". Now I assume there's a much easier way, like an append or add or put function for Arrays, but I can't seem to find such a function anywhere in the manuals. Is there some way I can just add each author to my $Book->Authors array? Here is the code I am attempting:
Thanks for your help!!
HawleyJR:Please use tags when Posting PHP Code In The Forums
I am trying to load Book objects with information from an XML file. I'm having problems loading my authors() array in my books object with Authors from the XML file if there is more than one <Author></Author> within the <Book> node. I seem to be stupid when it comes to Arrays! I'm having trouble in my foreach loop for Authors. Currently, I cannot even write the author name into my $authors array I created here, it gives me a "Can't use function return value in write context". Now I assume there's a much easier way, like an append or add or put function for Arrays, but I can't seem to find such a function anywhere in the manuals. Is there some way I can just add each author to my $Book->Authors array? Here is the code I am attempting:
Code: Select all
//set up an array to hold multiple Book objects
$Books = array();
$i = 0;
//go through each Book attributes listing and assign each node to Book object variables
foreach($xml->Items->Item->ItemAttributes as $Items) {
$Books[$i] = new Book();
$Books[$i]->ISBN = $Items->ISBN;
$Books[$i]->title = $Items->Title;
$Books[$i]->publisher = $Items->Publisher;
$Books[$i]->datePub = $Items->PublicationDate;
$Books[$i]->listPrice = $Items->ListPrice->FormattedPrice;
$k = 0;
$authors = array();
foreach($Items->Author as $name => $node) {
$authors($k) = ($node);
$k++;
}
$Books[$i]->authors = $authors;
$i++;
}HawleyJR:Please use tags when Posting PHP Code In The Forums