简体   繁体   English

XML多维数组,排序和引用

[英]XML Multidimensional Array, Sort and reference

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). 我有一个XML文件,我需要阅读和显示该文件,并带有使用PHP进行排序的选项,然后将必填字段输出到我的页面中。 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: 我设法获取了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>"; ?>

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? 首先,我用来阅读XML的代码是否很好? 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: 好的,所以这是我现在建议使用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/>";

The code you have written for reading the XML is absolutely fine. 您编写的用于读取XML的代码绝对不错。

However, you are unnecessarily converting from a SimpleXMLElement object to an array, as SimpleXMLElement is already iterable. 但是,您不必要将SimpleXMLElement对象转换为数组,因为SimpleXMLElement已经可以迭代。 You can access the <hotel> element of the XML as simply as: 您可以像下面这样简单地访问XML的<hotel>元素:

$Details->hotel[0]

You can iterate through the child elements of <hotel> by the following: 您可以通过以下方式遍历<hotel>的子元素:

<?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. 在这里, $index将是数字索引,而$hotel将是另一个SimpleXMLElement。

NB 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. PHP中的所有排序功能都适用于数组, 而不适用于对象。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM