下面是我的代码中的数组格式的两个示例,完整代码和数组内容。
阵列 1
Array
(
[version] => 1.0
[injuries] => Array
(
[timestamp] => 1377702112
[week] => 1
[injury] => Array
(
[0] => Array
(
[status] => Questionable
[id] => 10009
[details] => Shoulder
)
[1] => Array
(
[status] => Questionable
[id] => 10012
[details] => Ankle
)
阵列 2
Array
(
[version] => 1.0
[players] => Array
(
[timestamp] => 1377676937
[player] => Array
(
[0] => Array
(
[position] => TMDL
[name] => Bills, Buffalo
[id] => 0251
[team] => BUF
)
[1] => Array
(
[position] => TMDL
[name] => Colts, Indianapolis
[id] => 10009
[team] => IND
)
我需要做的是对两个数组进行排序并找到 ID 键的匹配值。然后我需要将两个数组的值合并到一个数组中,这样我就可以在屏幕上打印出来。提供了两个 API,一个用于带有球员 [id] 键的伤病报告,另一个用于带有 [id] 键的球员信息。这是我在这个问题上的进展:
<?php
function injuries_report() {
//Get json files
$injuryData = file_get_contents('http://football.myfa...=1&callback=');
$playerData = file_get_contents('http://football.myfa...L=&W=&JSON=1');
//format json data into an array
$obj1 = json_decode($injuryData, true);
$obj2 = json_decode($playerData, true);
//return print_r($obj1); //print obj1 to see output
return print_r($obj2); //print obj2 to see output
}
?>
<!--get injuries report -->
<pre><?php injuries_report(); ?></pre>
这是工作代码,感谢克里斯:)
$injuryData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=injuries&L=&W=&JSON=1&callback=');
$array1 = json_decode($injuryData, true);
$playerData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=players&L=&W=&JSON=1');
$array2 = json_decode($playerData, true);
function map($x) {
global $array1;
if(isset($x['id'])) {
$id = $x['id'];
$valid = array_filter($array1['injuries']['injury'], create_function('$injury', 'return $injury["id"] == "' . $id .'";'));
if(count($valid) > 0) {
$x = array_merge($x, array_shift($valid));
}
}
return $x;
}
$output = array_map('map', $array2['players']['player']);
echo "<pre>";
print_r($output);
echo "</pre>";