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?
The topic ‘How do I display a multidimensional array from a json api in wordpress?’ is closed to new replies.