I have an XML document called library.xml and I use the PHP function simplexml_load_file() to get a XML object that I can work with. I have a function called traverseObject() which goes through the document and prints out a nested list based on the document.
One of the tags that gets printed out in this nested list is the id tag. I'm trying to collect all the id tags into one array variable called $ids so that I can use them to generate another section of the page. The problem is that when I try to add these ids to the array, it is empty. I've tried just adding the number 5 to the array repeatedly, but it still doesn't work.
Here is what the simplified (for debugging) program looks like:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<div>Test</div>
<?php
$library = simplexml_load_file('095209376/library.xml');
$ids = array();
function traverseObject($object, $map)
{
echo "<ul>";
foreach ($object as $key => $value ){
if($key == "title"){
echo "<li><b>" . $object->title . "</b></li>";
} else if ($key == "topic"){
// traverse child elements, if any
traverseObject($value, $map);
} else if($key == "id"){
echo "<i>id: " . $value . "</i>";
$map[] = 5; ** <-------- this is the relevant part
} else { // skip the rest for now
}
}
echo "</ul>";
}
traverseObject($library, $ids);
if($ids == null){
echo "uh oh";
} else {
echo "whaaa";
foreach ( $ids as $key => $value ){
echo "key: " . $key . ", value: " . $value;
}
}
?>
</body>
</html>
It should just add a bunch of 5's to the array $ids and then print them out, but as you can see the array `$ids$ is null. Here's what gets printed:

I've looked over the code a bunch of times now, but I can't see anything wrong with it.