Это расстраивает меня! Это кажется таким простым, но я не могу пройтись по этому XML на всю жизнь. Я получаю обратно объект SimpleXMLElement по этой ссылке (http://www.nfl.com/liveupdate/scorestrip/ss.xml). Я пытаюсь перейти к массиву g и получить атрибуты [0], [1], [2] и т. Д. Я просто изучаю PHP. Так что любая помощь будет оценена.
Код, который я пробовал:
$nfl = simplexml_load_file('http://www.nfl.com/liveupdate/scorestrip/ss.xml');
foreach ($nfl as $game) {
print '<pre>';
print_r($game);
print '</pre>';
}
Это выводит:
SimpleXMLElement Object
(
[@attributes] => Array
(
[w] => 2
[y] => 2014
[t] => R
[gd] => 1
[bph] => 103
)
[g] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091100
[gsis] => 56186
[d] => Thu
[t] => 8:25
[q] => F
[h] => BAL
[hnn] => ravens
[hs] => 26
[v] => PIT
[vnn] => steelers
[vs] => 6
[rz] => 0
[ga] =>
[gt] => REG
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091400
[gsis] => 56187
[d] => Sun
[t] => 1:00
[q] => F
[h] => BUF
[hnn] => bills
[hs] => 29
[v] => MIA
[vnn] => dolphins
[vs] => 10
[rz] => 0
[ga] =>
[gt] => REG
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091401
[gsis] => 56188
[d] => Sun
[t] => 1:00
[q] => F
[h] => CAR
[hnn] => panthers
[hs] => 24
[v] => DET
[vnn] => lions
[vs] => 7
[rz] => 0
[ga] =>
[gt] => REG
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091402
[gsis] => 56189
[d] => Sun
[t] => 1:00
[q] => F
[h] => CIN
[hnn] => bengals
[hs] => 24
[v] => ATL
[vnn] => falcons
[vs] => 10
[rz] => 0
[ga] =>
[gt] => REG
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091403
[gsis] => 56190
[d] => Sun
[t] => 1:00
[q] => F
[h] => CLE
[hnn] => browns
[hs] => 26
[v] => NO
[vnn] => saints
[vs] => 24
[rz] => 0
[ga] =>
[gt] => REG
)
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091404
[gsis] => 56191
[d] => Sun
[t] => 1:00
[q] => F
[h] => MIN
[hnn] => vikings
[hs] => 7
[v] => NE
[vnn] => patriots
[vs] => 30
[rz] => 0
[ga] =>
[gt] => REG
)
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091405
[gsis] => 56192
[d] => Sun
[t] => 1:00
[q] => F
[h] => NYG
[hnn] => giants
[hs] => 14
[v] => ARI
[vnn] => cardinals
[vs] => 25
[rz] => 0
[ga] =>
[gt] => REG
)
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091406
[gsis] => 56193
[d] => Sun
[t] => 1:00
[q] => F
[h] => TEN
[hnn] => titans
[hs] => 10
[v] => DAL
[vnn] => cowboys
[vs] => 26
[rz] => 0
[ga] =>
[gt] => REG
)
)
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091407
[gsis] => 56194
[d] => Sun
[t] => 1:00
[q] => F
[h] => WAS
[hnn] => redskins
[hs] => 41
[v] => JAC
[vnn] => jaguars
[vs] => 10
[rz] => 0
[ga] =>
[gt] => REG
)
)
[9] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091408
[gsis] => 56195
[d] => Sun
[t] => 4:05
[q] => F
[h] => SD
[hnn] => chargers
[hs] => 30
[v] => SEA
[vnn] => seahawks
[vs] => 21
[rz] => 0
[ga] =>
[gt] => REG
)
)
[10] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091409
[gsis] => 56196
[d] => Sun
[t] => 4:05
[q] => F
[h] => TB
[hnn] => buccaneers
[hs] => 17
[v] => STL
[vnn] => rams
[vs] => 19
[rz] => 0
[ga] =>
[gt] => REG
)
)
[11] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091410
[gsis] => 56197
[d] => Sun
[t] => 4:25
[q] => F
[h] => DEN
[hnn] => broncos
[hs] => 24
[v] => KC
[vnn] => chiefs
[vs] => 17
[rz] => 0
[ga] =>
[gt] => REG
)
)
[12] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091411
[gsis] => 56198
[d] => Sun
[t] => 4:25
[q] => F
[h] => GB
[hnn] => packers
[hs] => 31
[v] => NYJ
[vnn] => jets
[vs] => 24
[rz] => 0
[ga] =>
[gt] => REG
)
)
[13] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091412
[gsis] => 56199
[d] => Sun
[t] => 4:25
[q] => F
[h] => OAK
[hnn] => raiders
[hs] => 14
[v] => HOU
[vnn] => texans
[vs] => 30
[rz] => 0
[ga] =>
[gt] => REG
)
)
[14] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091413
[gsis] => 56200
[d] => Sun
[t] => 8:30
[q] => F
[h] => SF
[hnn] => 49ers
[hs] => 20
[v] => CHI
[vnn] => bears
[vs] => 28
[rz] => 0
[ga] =>
[gt] => REG
)
)
[15] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091500
[gsis] => 56201
[d] => Mon
[t] => 8:30
[q] => P
[h] => IND
[hnn] => colts
[hs] => 0
[v] => PHI
[vnn] => eagles
[vs] => 0
[rz] => 0
[ga] =>
[gt] => REG
)
)
)
)
Я пробовал этот foreach, но он ничего не выводил:
foreach ($nfl->g as $game) {
print '<pre>';
print_r($game);
print '</pre>';
}
$nfl->g
не существует, и вам также нужно получить атрибуты, а не содержимое, так как они все пустые. Вы должны иметь успех со следующим. Обратите внимание на правильную глубину в XML $nfl->gms->g
, Также обратите внимание на использование attributes()
вызов.
$nfl = simplexml_load_file('http://www.nfl.com/liveupdate/scorestrip/ss.xml');
print '<pre>';
foreach ($nfl->gms->g as $game) {
foreach($game->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
echo "\n------------------------------------------\n";
}
print '</pre>';
Других решений пока нет …