keenProgrammer
keenProgrammer

Reputation: 438

Trouble accessing array return types

I'm trying to access my array values by calling them from another script.

I construct an array from my countryConfig.xml file. var_dump confirms that the values have been added successfully.

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

No results are displayed. Any ideas where I'm going wrong? I'm intending to print out the countries.

Upvotes: 1

Views: 37

Answers (1)

Vitamin
Vitamin

Reputation: 1526

Your second code block should be something like:

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

Edit: On a second view, your first code block seems to be incorrect as well. You should return $countries

Upvotes: 1

Related Questions