私が知る限り、動作するはずのPHP配列に問題があります。私は simpleXML で作業しており、simpleXML 出力をループしています。次に、関連する XML ノードから「id」属性を取得し、それを配列内の新しい項目のキーとして割り当て、その値を国名として割り当てようとしています。これが私の simpleXML 出力のサンプルです ($cxml
以下のコード内):
SimpleXMLElement Object (
[country] => Array (
[0] => SimpleXMLElement Object (
[@attributes] => Array ( [id] => AD )
[name] => ANDORRA
[ssc] => EUR
)
[1] => SimpleXMLElement Object (
[@attributes] => Array ( [id] => AE )
[name] => UNITED ARAB EMIRATES
[ssc] => EUR
)
[2] => SimpleXMLElement Object (
[@attributes] => Array ( [id] => AF )
[name] => AFGHANISTAN
[ssc] => ASI
) ...
)
など。これが私のコードです:
function generateCountryList() {
global $server_path;
// the following line generates - correctly - the object I gave above
$cxml = simplexml_load_file($server_path . 'countries/');
$c = array();
foreach ($cxml->country as $cntry => $rec) {
$rid = $rec['id'];
$rname = ucwords(strtolower($rec->name));
//the following echo statements are for debugging only
echo $rid; //returns the country ID; for example, AD on the 0th element in the object
echo $rname; //returns the country name; for example, Andorra on the 0th element in the object
$c[$rid] = $rname; //the goal here is to have an array of the form
//['AD'=>'Andorra','AE'=>'United Arab Emirates',...]
}
return $c;
}
返し$c
て変数に代入すると、print_r
その変数は空の配列になります。print_r($c);
この関数内で実行すると、同じことが得られます。
この配列を構築できない理由について誰かが提供できる助けをいただければ幸いです!