将 XML 文件元素转换为 PHP 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16372111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:00:11  来源:igfitidea点击:

Converting XML file elements into a PHP array

phpxmlsteam

提问by liamp47

I'm trying to convert a Steam group members list XML file into an array by using Php. The XML file is: http://steamcommunity.com/groups/starhawk/memberslistxml/?xml=1.xml

我正在尝试使用 Php 将 Steam 组成员列表 XML 文件转换为数组。XML 文件是:http: //steamcommunity.com/groups/starhawk/memberslistxml/?xml= 1.xml

The elements are the member's steam IDs, such as 76561198000264284

元素是会员的steam ID,比如76561198000264284

How can I go about doing this?

我该怎么做呢?

Edit: So far I've used something like this:

编辑:到目前为止,我已经使用了这样的东西:

$array = json_decode(json_encode((array)simplexml_load_string($xml)),1); 

It outputs the first few elements, not ones specifically from

它输出前几个元素,而不是专门来自

回答by Kartik

This should return the fully accessible array:

这应该返回完全可访问的数组:

$get = file_get_contents('http://steamcommunity.com/groups/starhawk/memberslistxml/?xml=1.xml');
$arr = simplexml_load_string($get);
print_r($arr);

You can now access items like this:

您现在可以访问这样的项目:

echo $arr->groupID64;
echo $arr->members->steamID64; 

Edit: To parse the streamID, you can do a for loop

编辑:要解析 streamID,您可以执行 for 循环

$ids = $arr->members->steamID64;
foreach($ids as $id) {
    echo $id;
}

回答by liyakat

you can use below functional code to get your correct answer

您可以使用以下功能代码来获得正确答案

<?php
$getfile = file_get_contents('http://steamcommunity.com/groups/starhawk/memberslistxml/?xml=1.xml');

$arr = simplexml_load_string($getfile);

foreach($arr->members->steamID64 as $a => $b) {
    echo "<br>".$a,'="',$b,"\"\n";
}
?>

OUTPUT

输出

steamID64="76561198009904532" 
steamID64="76561198004808757" 
steamID64="76561198000264284" 
steamID64="76561198016710420" 
steamID64="76561198005429187" 
steamID64="76561198030184436" 
steamID64="76561197980763372" 
steamID64="76561197972363016" 
steamID64="76561198045469666" 
steamID64="76561198010892015" 
steamID64="76561198028438913" 
steamID64="76561197967117636" 
steamID64="76561197980283206" 
steamID64="76561197992198727" 
steamID64="76561198018960482" 
steamID64="76561198071675315" 
steamID64="76561198010447988" 
steamID64="76561198025628761"

if you wish to customize you can make it as per your need. let me know if i can help you more..

如果你想定制,你可以根据你的需要制作。让我知道我是否可以帮助你更多..