Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-11
2
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 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 2000 total points
ID: 24353119
BTW, if you want info on "each" then see  http://www.php.net/each
0

Featured Post

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 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