开发者

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). 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

开发者_Go百科
[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/>";


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜