2015-02-08 150 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" => ""); ?> 
相關問題