Drupal - 从数组中获取值

时间:2015-03-23 18:11:30

标签: php arrays json drupal drupal-7

也许是我,但我无法从以下数组中获取值:

http://picpaste.com/pics/Untitled-2YV5V2Im.1427134235.png

我想要的是创建一个表格,其中标题是这样的:

HomeTeam名称| AwayTeam名称|匹配home_goals |匹配away_goals

然后我有9行值。

到目前为止我的代码:

$json = json_decode($server_output, true);
$days= $json['Calendar']['championship']['StageRound'][0]['matches'];

$header = ['HomeTeam name', 'AwayTeam name', 'Match home_goals', 'Match away_goals'];
$row = array();

foreach ($days as $key => $value) {
  ... here, I get always an error saying 'HomeTeam' is not an index...
}

$table = theme('table', array('header' => $header, 'rows' => $rows));

return $table;

有任何帮助吗? 谢谢!

编辑:

添加了以下代码:

foreach ($days as $key => $value) {
  $hometeam = $days[0]['HomeTeam']['name'];
  $awayteam= $days[0]['AwayTeam']['name'];
  dpm($hometeam . ' - ' . $awayteam);
}

我在for循环中的两行中都有索引[0],但我需要它从0到9(数组的长度。这样可以解决我的问题。

1 个答案:

答案 0 :(得分:0)

Foreach已经遍历该matches数组,因此您不需要[0]。跟着:

  $hometeam = $value['HomeTeam']['name'];
  $awayteam= $value['AwayTeam']['name'];

  $hometeam = $days[$key]['HomeTeam']['name'];
  $awayteam= $days[$key]['AwayTeam']['name'];

如果您想通过密钥访问数组...