Reputation: 5934
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 id
s 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.
Upvotes: 1
Views: 1227
Reputation: 821
As @Jack said, your problem here is that you are not passing the array by reference. But the reason is not correct. Unlike objects, PHP actually transfers scalar variables (arrays, strings, integers, floats, etc.) by making a copy of the variable whenever you are passing one in a function, or in a loop. This means that both of the following snippets will not modify the value.
function traverseObject($object, $map) {
// ...
}
foreach ($map as $key => $value) {
// ...
}
will actually make a new copy of the value, which is different from the one you are manipulating at the top. Thus, you need to pass the value as a reference.
function traverseObject($object, &$map) {
// ...
}
foreach ($map as $key => &$value) {
// ...
}
In the case of the function argument, a simple solution could be to manipulate an object instead of an array, as objects are always transferred by reference, and not by copy.
Upvotes: 1
Reputation: 173562
You need to use references in the function declaration; otherwise $map
will be treated as a function local variable.
function traverseObject($object, &$map)
^
Upvotes: 4