Solved

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

Posted on 2009-05-11
2
396 Views
Last Modified: 2012-08-14
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
Comment
Question by:R7AF
  • 2
2 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24353111
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
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 500 total points
ID: 24353119
BTW, if you want info on "each" then see  http://www.php.net/each
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
preventing spam with mailto 22 29
Macro to import XML in Access 2013 2 34
mysql date time 14 28
Cookie not unsetting 7 18
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question