为 SimpleXMLElements 数组编写 foreach 循环

发布于 2024-08-29 10:18:09 字数 954 浏览 4 评论 0原文

我正在使用 PHP 5 中的 XPath 来解析 XML 文档。我遇到的问题是编写 foreach 来正确显示以下数组:

XML 文档示例

值1 值2

           $xmlfile = 'link_to_file.xml';                       
            $xmlRaw = file_get_contents($xmlfile);      
            $xml = new SimpleXMLElement($xmlRaw);
            $install_files = $xml->xpath('//files');
            foreach($install_files as $row)
            {
              echo $row['file'];
            }

//var_dump for $row 给出以下内容

<前><代码> 数组(1) { [0] => 对象(SimpleXMLElement)#21 (2) { [“文件”]=> 字符串(12)“值1” [“文件夹”]=> 字符串(8)“值2” } }

理想情况下,我想通过使用 $row['file']$row['folder'] 获取该值。感谢您的任何帮助。

I am using the XPath in PHP 5 to parse a XML document. The problem I have is writing a foreach to correctly display the following array:

XML document sample

value 1
value 2

           $xmlfile = 'link_to_file.xml';                       
            $xmlRaw = file_get_contents($xmlfile);      
            $xml = new SimpleXMLElement($xmlRaw);
            $install_files = $xml->xpath('//files');
            foreach($install_files as $row)
            {
              echo $row['file'];
            }

//var_dump for $row gives the following

               array(1) {
                  [0]=>
                  object(SimpleXMLElement)#21 (2) {
                    ["file"]=>
                    string(12) "value 1"
                    ["folder"]=>
                    string(8) "value 2"
                  }
                }

Ideally I would like to get the value by using $row['file'] or $row['folder']. Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳公公是暖光 2024-09-05 10:18:09

数组中的项目是 SimpleXMLElement 对象。您可以使用 $object-> 访问它们的属性 ;属性 语法。所以试试这个:

foreach ($array as $row) {
    echo $row->folder . $row->file;
}

The items in your array are SimpleXMLElement objects. You can access their properties with the $object->property syntax. So try this:

foreach ($array as $row) {
    echo $row->folder . $row->file;
}
远山浅 2024-09-05 10:18:09

如果我正确理解你的例子,

foreach ($array as $row)
{
    // do something with $row['file'] or $row['folder']
}

If I understand your example correctly,

foreach ($array as $row)
{
    // do something with $row['file'] or $row['folder']
}
南城旧梦 2024-09-05 10:18:09

如果您只想处理一个元素,则不一定需要循环,但它也没有坏处。
根据您的输出,filefolder 是为 xpath 查询返回的元素的子元素。您可以通过 $parentElement->childElement 访问它们。如果它们是属性,您可以使用 $element['attributeName']

$foo = new SimpleXMLElement(getData());

foreach( $foo->xpath('row[@id="r2"]') as $row ) {
  echo $row->file, " | ", $row->folder, "\n";
}
echo "-----\n";

// same thing if you have more than one element in your result set
foreach( $foo->xpath('row[@id="r2" or @id="r3"]') as $row ) {
  echo $row->file, " | ", $row->folder, "\n";
}

function getData() {
  return '<foo>
    <row id="r1">
      <file>value 1.1</file>
      <folder>value 1.2</folder>
    </row>
    <row id="r2">
      <file>value 2.1</file>
      <folder>value 2.2</folder>
    </row>
    <row id="r3">
      <file>value 3.1</file>
      <folder>value 3.2</folder>
    </row>
  </foo>';
}

打印

value 2.1 | value 2.2
-----
value 2.1 | value 2.2
value 3.1 | value 3.2

If you want to process only one element you don't necessarily need a loop, but it doesn't hurt either.
According to your output file and folder are child-elements of the element returned for your xpath query. You can access them via $parentElement->childElement. If they were attributes you'd use $element['attributeName']

$foo = new SimpleXMLElement(getData());

foreach( $foo->xpath('row[@id="r2"]') as $row ) {
  echo $row->file, " | ", $row->folder, "\n";
}
echo "-----\n";

// same thing if you have more than one element in your result set
foreach( $foo->xpath('row[@id="r2" or @id="r3"]') as $row ) {
  echo $row->file, " | ", $row->folder, "\n";
}

function getData() {
  return '<foo>
    <row id="r1">
      <file>value 1.1</file>
      <folder>value 1.2</folder>
    </row>
    <row id="r2">
      <file>value 2.1</file>
      <folder>value 2.2</folder>
    </row>
    <row id="r3">
      <file>value 3.1</file>
      <folder>value 3.2</folder>
    </row>
  </foo>';
}

prints

value 2.1 | value 2.2
-----
value 2.1 | value 2.2
value 3.1 | value 3.2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文