Solved

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

Posted on 2009-05-11
2
373 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
Comment Utility
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
Comment Utility
BTW, if you want info on "each" then see  http://www.php.net/each
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
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.
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…
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.

771 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

9 Experts available now in Live!

Get 1:1 Help Now