Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 访问数组返回类型时出现问题_Php - Fatal编程技术网

Php 访问数组返回类型时出现问题

Php 访问数组返回类型时出现问题,php,Php,我试图通过从另一个脚本调用数组值来访问它们 我从我的countryConfig.xml文件构造了一个数组变量转储确认已成功添加值 countryConfig.php <?php $configFile = 'countryConfig.xml'; function getCountryConfig($configFile) { $xml = new SimpleXmlElement(file_get_contents("countryConfig.xml")); $c

我试图通过从另一个脚本调用数组值来访问它们

我从我的
countryConfig.xml
文件构造了一个数组<代码>变量转储确认已成功添加值

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];
?>


不显示任何结果。知道我哪里出错了吗?我打算把国家打印出来

第二个代码块应该类似于:

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

您的第二个代码块应该类似于:

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

返回数组($cities[0],$cities[1])返回数组…?代码中没有
$cities
变量<代码>返回数组($countries[0],$countries[1])
在您的示例中与
返回$countries
相同,请原谅
$cities
打字错误。仍然返回数组,数组使用
返回$countries
。只需打印索引0处的值,然后打印索引1。使用上面和下面的
返回数组($cities[0],$cities[1])返回数组…?代码中没有
$cities
变量<代码>返回数组($countries[0],$countries[1])
在您的示例中与
返回$countries
相同,请原谅
$cities
打字错误。仍然返回数组,数组使用
返回$countries
。只需打印索引0处的值,然后打印索引1。