Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

[Simplexml] please explain: while(list( , $node) = each($result))

The php.net page about simplexml and xpath has an example (see below or http://nl3.php.net/manual/en/function.simplexml-element-xpath.php), but I would like to see some explanation of part of the code.

while(list( , $node) = each($result))

1) Can someone explain the list function?
2) Is it possible to get $node back as another xml-string?
3) Why can't I use an expression like $xml->xpath('b/c') directly for output?
<?php
$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;
 
$xml = new SimpleXMLElement($string);
 
/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');
 
while(list( , $node) = each($result)) {
    echo '/a/b/c: ',$node,"\n";
}
 
/* Relative paths also work... */
$result = $xml->xpath('b/c');
 
while(list( , $node) = each($result)) {
    echo 'b/c: ',$node,"\n";
}
?>

Open in new window

0
R7AF
Asked:
R7AF
  • 2
2 Solutions
 
Beverley PortlockCommented:
List() is used for grabbing array values and assigning them to ordinary variables like so

while (list($id, $name, $salary) = mysql_fetch_row($result)) {
    echo " <tr>\n" .
          "  <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
          "  <td>$salary</td>\n" .
          " </tr>\n";
}

so instead of returning an array with three elements, the values are inserted into the three variables. See http://www.php.net/list for more examples.
0
 
Beverley PortlockCommented:
BTW, if you want info on "each" then see  http://www.php.net/each
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now