I have an XML file that (in a simplified version) looks like this:
<franchises>
<franchise>
<teamname>Some name</teamname>
<roster>
<player>
<name>Player name</name>
<dob>19800312</dob>
</player>
<player>
<name>Player name</name>
<dob>19900506</dob>
</player>
//Repeated for another X times pr roster
</roster>
</franchise>
//Repeated for another 29 times
</franchises
I've written separate classes for Franchise and Player. The Franchise-class contains an array of Player. What I'm trying to achieve, is create an array that holds the franchises, and and for each Franchise add each respective Player to that Franchise. I've written this function, and the problem is that it adds all players to each Franchise, ie. also players from other franchises:
function load_XML($_filename) {
$franchises = simplexml_load_file($_filename);
$franchise_array = array();
foreach($franchises->franchise as $franchise) {
$franchise_array[] = new Franchise((string) $franchise->teamname);
foreach($franchise->roster->player as $player) {
$new_player[] = new Player((string) $player->name,
(string) $player->dob);
$franchise_array[(count($franchise_array) - 1)]->add_player($new_player);
}
}
return $franchise_array;
}
Obviously the error is in the latter foreach-loop, but I can't identify what's wrong with it.
No comments:
Post a Comment