Skip to content
Advertisement

Foreach array inside of array

I have a JSON data that looks like this (link for full response here https://pastebin.com/LG2F9Vrw)

"data": [ { "matchId": 1653309, "personId": 1141434, "teamId": 89736, "competitors": [ { "teamCode": "SHC", "website": "", } ] },

There’s an array of ['data'] that I’m using with foreach to give me game statistics. There’s now a second array inside of the ['data'] array. I’mn trying to get the ['teamCode'] string to print but I can’t work out how to do it.

I’ve done my best following tutorials online.

<?php foreach($json3['response']['data'] as $item) {
    
print '<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
       <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
       <div data-v-7429a5ba="" class="h-full border-team-'; 
foreach($json3['response']['data']['competitors'] as $row => $value) {
  print $value['teamCode'];
}">';
?>

There are other areas where I’m using print $item['sFieldGoalsPercentage']; but that’s coming under the first foreach.

Thanks!

Advertisement

Answer

this is simple code how to retreive ['teamCode'] you want (test link):

$json3 = json_decode($v,true); // $v is the json code that referred via the link you mentioned
foreach($json3['response']['data'] as $item) {
    echo '<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
          <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">';
    foreach ($item["competitors"] as $row){
        echo '<div data-v-7429a5ba="" class="h-full border-team-'.$row["teamCode"].'"></div>';
    }
    echo '</td></tr>';
}

this is the output:

<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
  <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
    <div data-v-7429a5ba="" class="h-full border-team-SHC"></div>
  </td>
</tr>
<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
  <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
    <div data-v-7429a5ba="" class="h-full border-team-KBA"></div>
  </td>
</tr>
<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
  <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
    <div data-v-7429a5ba="" class="h-full border-team-HRE"></div>
  </td>
</tr>
<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
  <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
    <div data-v-7429a5ba="" class="h-full border-team-NEW"></div>
  </td>
</tr>
<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
  <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
    <div data-v-7429a5ba="" class="h-full border-team-UHC"></div>
  </td>
</tr>
<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
  <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
    <div data-v-7429a5ba="" class="h-full border-team-SHC"></div>
  </td>
</tr>
<tr data-v-7429a5ba="" class="border-b-2 border-gray-accent-100">
  <td data-v-7429a5ba="" class="relative lg:text-sm xxs:text-xs text-center">
    <div data-v-7429a5ba="" class="h-full border-team-KBA"></div>
  </td>
</tr>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement