php - 从xml读取数据并创建一个数组对象

时间:2015-02-08 06:48:39

标签: php arrays xml

我是php新手,我使用metro-websign创建我的网站。有一个插件采用阵列。以下代码工作正常:

<? php
$photoNewsPath = array(
  "photo/committees/spirit-rock/20150203-anna.jpg",
  "photo/news/20150207 - 100 day.jpg"
);

$photoNewsTitle = array("Post your photos on the website", "100 school day = pajama day fun");

var_dump($photoNewsPath);

$tile[] = array(
  "type" => "slideshow",
  "images" => $photoNewsPath,
  "classes" => "");

但是当我从xml文件中读取数组时:

<? php
$photonews = simplexml_load_file("config\photonews.xml") or die("Error: Cannot create object");

foreach($photonews - > news as $news) {
  $photoNewsPath[] = (string) $news - > path;
}

var_dump($photoNewsPath);

$tile[] = array(
  "type" => "slideshow",
  "images" => $photoNewsPath,
  "classes" => ""); ?>

该插件不再有效。我使用var_dump从两个代码片段中转储数组。结果是一样的。什么可以使数组不同,所以php插件失败?

任何线索?

1 个答案:

答案 0 :(得分:0)

你在foreach中有错误。这是正确的访问代码 <path></path>标记到xml

<?php
$photonews = simplexml_load_file("config/photonews.xml") or die("Error: Cannot create object");
$photoNewsPath=array();
foreach($photonews as $key => $value) {
$photoNewsPath[]= (string) $photonews->path;
}

// var_dump($photoNewsPath);

$tile[] = array(
  "type" => "slideshow",
  "images" => $photoNewsPath,
  "classes" => ""); ?>