2012-02-20 94 views
1

我想通過從另一個腳本調用它們來訪問我的數組值。無法訪問數組返回類型

我從我的countryConfig.xml文件構建一個數組。 var_dump確認這些值已成功添加。

countryConfig.php

<?php 
$configFile = 'countryConfig.xml'; 

function getCountryConfig($configFile) { 

    $xml = new SimpleXmlElement(file_get_contents("countryConfig.xml")); 

    $countries = array(); 

    $countryId = (string)($xml->city[0]->id); 
    array_push($countries, $countryId); 
    $countryId = (string)($xml->city[1]->id); 
    array_push($countries, $countryId); 

// var_dump($countries); 

    return array (
     $countryId[0], 
     $countryId[1] 
    ); 
} 
?> 

的index.php

<?php 
require 'includes/countryConfig.php'; 

$pageContent = 'The countries are' . $countryId[0] . ' and ' . $countryId[1]; 
?> 

未顯示結果。任何想法,我要去錯了嗎?我打算打印這些國家。

回答

1

你的第二個代碼塊應該是這樣的:

<?php 
require 'includes/countryConfig.php'; 
$countryId = getCountryConfig('countryConfig.xml'); 
$pageContent = 'The countries are ' . $countryId[0] . ' and ' . $countryId[1]; 
echo $pageContent; 

編輯:在第二個觀點,你的第一個代碼塊似乎是不正確的爲好。你應該return $countries

+0

通過以上和以下的'返回陣列( $城市[0], $城市[1] );'我回陣列陣列...? – keenProgrammer 2012-02-20 21:18:51

+1

代碼中沒有'$ cities'變量。 '返回數組($ countries [0],$ countries [1])'在你的例子中與'return $ countries'相同 – Vitamin 2012-02-20 21:21:32

+0

請原諒'$ cities'錯字。仍然使用'return $ countries'返回Array,Array。只需要在索引0處打印值,然後索引爲1. – keenProgrammer 2012-02-20 21:26:12