2015-02-08 147 views
1

我是新来的PHP,我使用metro-websign创建我的网站。有一个插件接受一个数组。下面的代码工作正常:php - 从XML读取数据并创建一个数组对象

<? 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插件失败?

任何线索?

+0

重写问题。他们有相反的意思。有问题的你说数组写入XML文件 - 但在问题的头你说你想读取XML文件,并从其内容创建数组。 – 2015-02-08 08:12:54

回答

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" => ""); ?>