【发布时间】:2017-11-08 21:08:41
【问题描述】:
我正在尝试使用来自 json api 的数据在 Wordpress 网站中为运动队创建一个固定装置列表或表格,但我在弄清楚如何做到这一点时遇到了问题。由于某些灯具具有相同的日期,我想通过执行以下操作将数据分组到一个新数组中:
$matchLists = json_decode(json_encode($fixture_data), True);
$newlist = array();
foreach ($matchLists as $key => $matchitem) {
if (array_key_exists('matchDate', $matchitem)) {
$newlist[$matchitem['matchDate']][$key] = ($matchitem);
}
}
但这似乎具有为数组创建进一步级别的效果,请参阅附图以查看 $newlist 的样子:newlist array example
我正在努力使用我迄今为止看到的关于处理多维数组的教程/示例来正确拆分这些信息。我可以使用以下方法获取固定日期标题:
foreach($newlist as $key => $value)
{
$fixtureDate = date('D j M Y ga', strtotime($key));
$return .= '<li><h4>' . $fixtureDate . $test . '</h4></li>';
}
但这并没有为我提供提取我需要的其余数据的途径。 这就是我在意识到我需要将灯具分组到正确的日期/时间之前获得信息的方式
$fixtureHome = json_decode(json_encode($fixtureitem->homeTeam), true);
$fixtureAway = json_decode(json_encode($fixtureitem->awayTeam), true);
$fixtureHomeName = $fixtureHome['name'];
$fixtureAwayName = $fixtureAway['name'];
$fixtureHomeID = $fixtureHome['id'];
$fixtureAwayID = $fixtureAway['id'];
$fixtureID = $fixtureitem->id;
$matchDate = $fixtureitem->matchDate;
$fixtureDate = date('D j M Y ga', strtotime($matchDate));
$homeColour = $fixtureHome['color'];
$awayColour = $fixtureHome['color'];
$fixtureStatus = json_decode(json_encode($fixtureitem->matchStatus), true);
$scoreHome = $fixtureStatus['homeTeamScore'];
$scoreAway = $fixtureStatus['awayTeamScore'];
但我不明白现在如何使用 $newlist 实现相同的结果?
【问题讨论】:
标签: php arrays json wordpress multidimensional-array