我们如何从这个关联数组中访问客队的比分? 下面是使用访问器在 PHP 中执行print_r时的关联数组的输出 $preGamesSorted[$e][away][score],但没有任何显示。
(
[0] => Array
(
[state] => final
[startTimeDisplay] => 12:00 PM ET
[startTimeEpoch] => 1426262400
[home] => Array
(
[rank] => 24
[name] => Davidson
[winner] => true
[record] => 24-6
[score] => 67
)
[away] => Array
(
[rank] => 0
[name] => La Salle
[winner] => false
[record] => 17-16
[score] => 66
)
)
这是 php 代码:
<?php
$url = file_get_contents("http://i.turner.ncaa.com/sites/default/files/external/test/scoreboard.json");
$turnerNCAAJsonObject = json_decode($url,true);
$liveGames = array();
$finalGames = array();
$preGames = array();
for ($n = 0; $n <= count($turnerNCAAJsonObject); $n++) {
if(isset($turnerNCAAJsonObject[$n][state]) && $turnerNCAAJsonObject[$n][state] == 'final') {
array_push($finalGames, $turnerNCAAJsonObject[$n]);
}elseif (isset($turnerNCAAJsonObject[$n][state]) && $turnerNCAAJsonObject[$n][state] == 'pre') {
array_push($preGames, $turnerNCAAJsonObject[$n]);
}elseif ($turnerNCAAJsonObject[$n][state] == 'live') {
array_push($liveGames, $turnerNCAAJsonObject[$n]);
}
}
print_r($liveGames);
$finalGamesSorted = value_sort($finalGames, 'startTimeEpoch');
echo "<font color='"red'"><h3>Sorted Final Games</h3></font>";
//start of display for sorted final games
for ($e = 0; $e <= count($finalGamesSorted); $e++) {
echo "<font color='"red'">";
echo "Game Status: " . strtoupper($finalGamesSorted[$e][state]);
echo "<br>";
echo $preGamesSorted[$e][home][name] . " (" . $preGamesSorted[$e][home][rank] . ")" . " -VERSUS- " . $preGamesSorted[$e][away][name] . " (" . $preGamesSorted[$e][away][rank] . ")";
echo "<br>";
echo "Home Team's Record: " . $preGamesSorted[$e][home][record] . " -VERSUS- Away Team's Record: " . $preGamesSorted[$e][away][record];
echo "<br>";
//print_r($preGamesSorted[$e][startTimeEpoch]);
echo "</font>";
echo "<h3>" . $preGamesSorted[$e][home][score] . "-" . $preGamesSorted[$e][away][score] . "</h3>";
//echo $preGamesSorted[$e][home][score];
foreach ($finalGamesSorted[$e] as $title => $gameSpecifics) {
foreach ($gameSpecifics as $stateKey => $stateValue){
echo "<b><u>";
print "$stateKey : $stateValue<br />";
echo "</b></u>";
}
echo "<br>";
}
echo "<br>";
}
?>
数组输出是在变量$finalGamesSorted上执行print_r时显示的内容。 此外,函数value_sort,按数组的一个数字键对数组进行排序。
您在数组密钥访问中忘记了"。在应该是
$preGamesSorted[$e]['home']['name'] // Home
$preGamesSorted[$e]['away']['name'] // Away