如何从XML文件解析数组


How to parse an array from XML file

执行VAR_DUMP()后,我有一个XML文件,这就是它返回的

["EditorialReviews"]=>
      object(stdClass)#120 (1) {
        ["EditorialReview"]=>
        array(2) {
          [0]=>
          object(stdClass)#121 (3) {
            ["Source"]=>
            string(19) "Product Description"
            ["Content"]=>
            string(610) "Description."
            ["IsLinkSuppressed"]=>
            bool(false)
          }
          [1]=>
          object(stdClass)#118 (3) {
            ["Source"]=>
            string(30) "Product Description"
            ["Content"]=> "Description 2"
          }
        }

我从来没有这样做过,这个XML文件里面有一个数组,我怎么能只得到第一个呢?通常我只会做

$this->EditorialReviews->EditorialReview->Content

但它会返回NULL,或者当有数组时它不会显示。

$this->EditorialReviews->EditorialReview->Content[0]

给我一个错误

此外,我尝试使用php的simpleXML,但它也给了我一个错误。。

试试这个:

$content = is_array($this->EditorialReviews->EditorialReview)?$this->EditorialReviews->EditorialReview[0]->Content:$this->EditorialReviews->EditorialReview->Content;
var_dump($content);