3
여러 도시의 정보를 보유 할 간단한 XML 문서를 만들었습니다.XML에서 요소를 추출하여 배열에 배치하려고 시도했습니다.
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<city>
<id>London</id>
</city>
<city>
<id>New York</id>
</city>
</config>
도시 요소를 추출하여 배열에 배치하려고합니다. 내가 지금까지 다음과 출력이 단순히 Array
때 함수를 호출합니다.
<?php
$configFile = 'cityConfig.xml';
function getCityConfig($configFile) {
$xml = new SimpleXmlElement(file_get_contents("cityConfig.xml"));
$cities = array();
$cityId = $xml->city[0]->id;
array_push($cities, $cityId);
$cityId = $xml->city[1]->id;
array_push($cities, $cityId);
//var_dump($cities);
return $cities;
}
//print_r(getCityConfig($configFile));
echo getCityConfig($configFile);
?>
var_dump
은 값이 배열에 추가되고 있음을 나타냅니다.
array(2) { [0]=> object(SimpleXMLElement)#2 (1) { [0]=> string(6) "London" } [1]=> object(SimpleXMLElement)#4 (1) { [0]=> string(8) "New York" } } Array
나는이 라인을 따라 뭔가를 달성하려고합니다.
$cities = array(
'London',
'New York',
'Paris'
);
배열 색인은 콘텐츠를 표시하기 위해 내 index.php에서 호출됩니다.
$pageIntroductionContent = 'The following page brings twin cities together. Here you will find background information on ' . $cities[0] . ', ' . $cities[1] . ' and ' . $cities[2] . '.';
내가 잘못 가고있는 아이디어가 있습니까?
미리 감사드립니다.
감사합니다. 나는 그 중 하나를 놓쳤습니다. 배열의 내용을 인쇄하는 것과 관련하여 내가 잘못 생각한 아이디어가 있습니까? – keenProgrammer
코드는 괜찮습니다. 내가 말한 것처럼 문자열을 캐스트해야합니다. 'echo getCityConfig (..)'에 대해서 말한다면, PHP에서는 이런 식으로 배열을 출력 할 수 없습니다. 'print_r' 또는'var_dump'를 사용해야합니다. 실제로 배열은 문자열이 아니므로 전체 배열을 출력하는 것은 의미가 없습니다. –
삭제 해 주셔서 감사합니다. 마지막 문제는 index.php 스크립트의 배열 인덱스 인'$ pageIntroductionContent '에 대한 액세스입니다. 내가'cityConfig.php가 필요하다면'index 0'과'Warning : file_get_contents (cityConfig.xml) [function.file-get-contents] : 스트림을 열지 못했습니다 .'를 호출 할 때 정의되지 않은 함수를 얻습니다. – keenProgrammer