I have a simple XML document containing two city id's.

我有一个包含两个城市ID的简单XML文档。

<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
    <city>
        <id>London</id>
    </city>
    <city>
        <id>New York</id>
    </city>
</config>

When iterating over the XML I can only pick up the first city id, e.g. London.

在迭代XML时,我只能获取第一个城市ID,例如伦敦。

<?php
$configFile = 'cityConfig.xml';

function getCityId($configFile) {

    $xml = new SimpleXmlElement(file_get_contents("cityConfig.xml"));

    $cities = array();

    foreach ($xml->city->id as $cityId) {
        $cityId = (string) $cityId;
        array_push($cities, $cityId);
    }

    return $cities;
}

print_r(getCityId($configFile));
?>

<?php

The output from the above:

以上输出:

// Array ( [0] => London )

I'm casting $cityId into a string to be used elsewhere in my website.

我正在将$ cityId转换为一个字符串,以便在我的网站的其他地方使用。

Any ideas where I'm going wrong?

我出错的任何想法?

Thanks in advance.

提前致谢。

1 个解决方案

#1


1

foreach ($xml->city->id as $cityId)

Should be:

应该:

foreach ($xml->city as $city) {
    $cityId = $city->id;
    ...
}

更多相关文章

  1. listview 通过筛选全部城市显示出搜索的城市
  2. Android利用IP地址定位城市(全世界可用)

随机推荐

  1. 如何解决App无法收到android开机广播
  2. 在Android中根据联系人查询电话号码
  3. windows下载android源代码
  4. Android中显示照片的Exif信息
  5. 高仿Android 点心桌面皮肤实现方式
  6. 【Android车载系统 News | Tech 3】News
  7. 博客园app for xamarin android一款简洁
  8. Android中的sqlite简单示例
  9. android studio 3.6.0 绑定视图新特性的
  10. Android XML 解析