Solved

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

Posted on 2009-05-11
2
379 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
Calculate values in an array 18 33
is this a cms? 8 34
How do I make my dropdowns depended within a JQuery Modal Dialog box. 3 10
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now