Search code examples
phparraysjsondrupaldrupal-7

Drupal - get value from array


Maybe it's me but I can't get the values from the following array:

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

What I want is to create a table where the headers are like this:

HomeTeam name | AwayTeam name | Match home_goals | Match away_goals

and then I have 9 rows with values.

My code so far:

$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;

Any help? Thanks!

EDIT:

Added this code:

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

I have the index [0] in both lines inside the for cycle but I need it to be from 0 to 9 (the lenght of the array. That would solve my problem.


Solution

  • Foreach is already iterating trough that matches array, so you don't need that [0]. Go with:

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

    or

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

    if you like to access array over key...