XML多维数组、排序和引用

发布于 2024-11-16 06:42:41 字数 1636 浏览 0 评论 0原文

我真的根本没有得到数组,我读过很多例子,大多数是抄袭和修改的,我还没有找到我理解的帮助,我确实希望有人能引导我完成这个,所以我完全理解。

我有一个 XML 文件,我需要使用 PHP 读取并显示该文件并提供排序选项,然后将所需字段输出到我的页面中。我已设法检索 XML 文件并在本地重新安置并检查文件的年龄(里程碑)并使用代码(下面)已设法读取 XML 文件并将其输出为数组,但我无法再进一步,并且在两次之后这几天我向社区寻求帮助:

<?php

 function std_class_object_to_array($stdclassobject) {
  $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;

    foreach ($_array as $key => $value) {
    $value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
     $array[$key] = $value;
    }
   return $array; 
  }

  $request = $domain.$filename;
  $API_results = file_get_contents($request);
  $xml = new SimpleXMLElement($API_results);
  $Details = std_class_object_to_array($xml);

echo "<pre>";
print_r($Details[hotel]);
echo "</pre>"; ?>

输出看起来像这样(缩短)

Array
(
    [0] => Array
        (
            [hotel_ref] => 157258
            [hotel_name] => Hotel Kong Arthur

继续

[1] => Array
    (
        [hotel_ref] => 98813
        [hotel_name] => Hotel Lautruppark 

首先,我用于读取 XML 的代码好吗?有没有更好的方法可以使用?如何对结果进行排序?如何输出各个字段?

非常感谢您的帮助、示例和指导,

Stu

//------------------------ 更新 ------------ ----------- \

好吧,这就是我现在建议使用 SimpleXML 的地方,但它仍然不对,而且我并没有进一步深入:

$request = $domain.$filename;
$xmlobj = simplexml_load_file($request);
$xml = simplexml_load_file($request) or die("NO XML");
foreach($xml as $hotels)
 echo $hotels->hotel_name." (".$hotels->hotel_ref.")<br/>";

I am REALLY not getting arrays at all, I have read many examples, most plagiarized and amended, am an yet to find help that I understand I do hope someone can walk me through this, so I understand fully.

I have an XML file, which I need to read and display with options to sort using PHP, then output the required fields into my page(s). I have managed to retreive the XML file and rehouse locally and check the age of the file (milestone) and using the code (below) have managed to read the XML file and output as an array, but I can get no further and after two days I turn to you the community for assistance:

<?php

 function std_class_object_to_array($stdclassobject) {
  $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;

    foreach ($_array as $key => $value) {
    $value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
     $array[$key] = $value;
    }
   return $array; 
  }

  $request = $domain.$filename;
  $API_results = file_get_contents($request);
  $xml = new SimpleXMLElement($API_results);
  $Details = std_class_object_to_array($xml);

echo "<pre>";
print_r($Details[hotel]);
echo "</pre>"; ?>

The Output looks like this (shortened)

Array
(
    [0] => Array
        (
            [hotel_ref] => 157258
            [hotel_name] => Hotel Kong Arthur

continued

[1] => Array
    (
        [hotel_ref] => 98813
        [hotel_name] => Hotel Lautruppark 

Firstly is the code I have used for reading the XML good? Is there a better way that could be used? How do I sort the results? How do I output the individual fields?

Your help, examples and direction would be greatly appreciated,

Stu

//----------------------- UPDATE ----------------------- \

OK, so this is where I am at now with the suggestion to use SimpleXML, but its still not right and I am not really a great deal further on:

$request = $domain.$filename;
$xmlobj = simplexml_load_file($request);
$xml = simplexml_load_file($request) or die("NO XML");
foreach($xml as $hotels)
 echo $hotels->hotel_name." (".$hotels->hotel_ref.")<br/>";

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

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

发布评论

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

评论(1

许你一世情深 2024-11-23 06:42:41

您编写的用于读取 XML 的代码绝对没问题。

但是,您不必要从 SimpleXMLElement 对象转换为数组,因为 SimpleXMLElement 已经是可迭代的。您可以访问 XML 的 元素,如下所示:

$Details->hotel[0]

您可以通过以下方式迭代 的子元素:

<?php
// Loop through the elements, and with each...
foreach($Details->hotel[0] as $index => $hotel) {
    // ...output the name of the hotel:
    echo $hotel->hotel_name;
}
?>

这里,$index 将是数字索引,$hotel 将是另一个 SimpleXMLElement。

NB 在对结果进行排序方面,实际上可能值得进行对象到数组的转换。 PHP 中的所有排序函数都适用于数组,而不是对象。

希望这有帮助!

The code you have written for reading the XML is absolutely fine.

However, you are unnecessarily converting from a SimpleXMLElement object to an array, as SimpleXMLElement is already iterable. You can access the <hotel> element of the XML as simply as:

$Details->hotel[0]

You can iterate through the child elements of <hotel> by the following:

<?php
// Loop through the elements, and with each...
foreach($Details->hotel[0] as $index => $hotel) {
    // ...output the name of the hotel:
    echo $hotel->hotel_name;
}
?>

Here, $index will be the numeric index, and $hotel will be another SimpleXMLElement.

N.B. In terms of sorting the results, it might actually be worth doing the object-to-array conversion. All of the sorting functions in PHP work on arrays, not objects.

Hope this helps!

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