Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用simplexml\u load\u字符串在XML上迭代_Php_Xml_Simplexml - Fatal编程技术网

Php 使用simplexml\u load\u字符串在XML上迭代

Php 使用simplexml\u load\u字符串在XML上迭代,php,xml,simplexml,Php,Xml,Simplexml,我正在尝试从XML文档中检索数据,该文档如下所示: <scores sport="soccer" updated="08.05.2018 09:39:42"> <category name="Argentina: Superliga" gid="1081" id="1081" file_group="argentina" iscup="False"> <matches date="May 08" formatted_date="08.05.2018"> <

我正在尝试从XML文档中检索数据,该文档如下所示:

<scores sport="soccer" updated="08.05.2018 09:39:42">
<category name="Argentina: Superliga" gid="1081" id="1081" file_group="argentina" iscup="False">
<matches date="May 08" formatted_date="08.05.2018">
<match status="FT" timer="" date="May 08" formatted_date="08.05.2018" time="00:15" commentary_available="argentina" venue="Estadio Brigadier General Estanislao López" v="60" static_id="2288469" fix_id="2172208" id="2442089">
<localteam name="Colon Santa FE" goals="0" id="5947"/>
<visitorteam name="River Plate" goals="0" id="6042"/>
<events>
<event type="yellowcard" minute="39" extra_min="" team="visitorteam" player="C. Mayada" result="" playerId="91623" assist="" assistid="" eventid="24420891"/>
<event type="yellowcard" minute="40" extra_min="" team="localteam" player="M. Fritzler" result="" playerId="14256" assist="" assistid="" eventid="24420892"/>
<event type="subst" minute="46" extra_min="" team="visitorteam" player="J. Quintero" result="" playerId="74510" assist="G. Martinez" assistid="51346" eventid="24420893"/>
<event type="subst" minute="63" extra_min="" team="visitorteam" player="L. Pratto" result="" playerId="17657" assist="R. Mora" assistid="55877" eventid="24420894"/>
<event type="subst" minute="63" extra_min="" team="localteam" player="N. Leguizamon" result="" playerId="443149" assist="C. Bernardi" assistid="186181" eventid="24420895"/>
<event type="subst" minute="70" extra_min="" team="localteam" player="P. Ledesma" result="" playerId="13875" assist="A. Bastia" assistid="15082" eventid="24420896"/>
<event type="subst" minute="73" extra_min="" team="visitorteam" player="R. Borre" result="" playerId="320836" assist="I. Scocco" assistid="15172" eventid="24420897"/>
<event type="subst" minute="82" extra_min="" team="localteam" player="L. Heredia" result="" playerId="337735" assist="A. Ruiz" assistid="189511" eventid="24420898"/>
<event type="yellowcard" minute="90" extra_min="1" team="visitorteam" player="R. Borre" result="" playerId="320836" assist="" assistid="" eventid="24420899"/>
</events>
<ht score="[0-0]"/>
<ft score="[0-0]"/>
</match>
</matches>
</category>
<category name="Argentina: Primera B Nacional - Promotion - Play Offs" gid="3899" id="1078" file_group="argentina" iscup="False">
<matches date="May 08" formatted_date="08.05.2018">
<match status="23:05" timer="" date="May 08" formatted_date="08.05.2018" time="23:05" commentary_available="" venue="" v="0" static_id="2369960" fix_id="2253925" id="2443571">
<localteam name="Almagro" goals="?" id="5900"/>
<visitorteam name="Agropecuario" goals="?" id="21385"/>
<events/>
<ht score=""/>
</match>
</matches>
</category>
</scores>
我得到的信息是:

Notice: Trying to get property of non-object in C:\xampp\htdocs\proj\inc\get.php on line 23

Notice: Trying to get property of non-object in C:\xampp\htdocs\proj\inc\get.php on line 23

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\proj\inc\get.php on line 23
具体来说,我试图提取属性中的内容,如日期、团队名称、目标等


提前感谢。

使用SimpleXML加载XML文件时,根元素是任何访问的起点。因此,您不需要
分数
,因为这是
$xml

foreach ($xml->category->matches->match as $value) {
    echo (string)$value['status'];
}
这将输出status元素(
FT
)以显示它实际上是
元素

更新:正如Syscall提到的,这个XML的结构需要类似于

foreach ($xml->category as $value1) {
    foreach ( $value1->matches->match as $value)    {
        echo (string)$value['status'];
    }
}

要获取所有
元素。

@Syscall,它们可以像您所说的那样作为两个循环执行,也可以使用XPath。这取决于他们是真的想要全部还是其他东西。
foreach ($xml->category as $value1) {
    foreach ( $value1->matches->match as $value)    {
        echo (string)$value['status'];
    }
}