Support » Developing with WordPress » How do I display a multidimensional array from a json api in wordpress?

  • I am trying to create either a list or table of fixtures for sports teams in a WordPress website using data that comes in from a json api and I am running into issues figuring out exactly how to do this. As some of the fixtures have the same dates, I thought to group the data into a new array by doing the following:

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

    But this seems to have the effect of creating a further level to the array please see picture attached to see what the $newlist looks like: newlist array example

    I am struggling to use the tutorials/examples I have seen so far on dealing with multi dimensional arrays to split this information out correctly. I can get the fixture date headings using:

     foreach($newlist as $key => $value)
         {
          $fixtureDate = date('D j M Y ga', strtotime($key));
    
    $return .= '<li><h4>' . $fixtureDate . $test . '</h4></li>';
    }
    

    but this doesn’t then offer me a route to pull out the rest of the data that I need. This is how I got the information before realizing that I would need to group the fixtures into the correct dates/times

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

    but I don’t understand how to achieve the same results now with the $newlist?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Pioneer Web Design

    (@swansonphotos)

    json_encode is used to send data in a json pack.

    json_decode is used to receive json data and handle it.

    Not at all clear what above is trying to achieve…

    Moderator bcworkz

    (@bcworkz)

    Decode the JSON output, which gives you a data array. Var_dump the array so you can see what you have to work with. Getting to the various elements is a matter of developing the right reference. Depending on the structure, it could be rather elaborate.
    echo $data[2]->foo['bar'][3]->snafu;

    thanks for your help, finally cracked it :-), i was a long the right lines before but not quite there so your example really helped me to figure out the structure.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.