2012-02-20 90 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