How to use "for" to work with data in an array whose amount of varied data is unknown?

Asked

Viewed 40 times

0

I have the following JSON variable resulting from a php query:

$json_string = '[
  {
    "id": 1,
    "name": "Jogos para PC",
    "products": [
      {
        "id": 34,
        "name": "Grand Theft Auto V"
      },
      {
        "id": 59,
        "name": "Bloodstained Ritual of the Night"
      },
      {
        "id": 60,
        "name": "F1 2019 - Anniversary Edition"
      },
      {
        "id": 62,
        "name": "PLAYERUNKNOWN\'S BATTLEGROUNDS"
      },
      {
        "id": 64,
        "name": "Batman: Arkham Knight"
      }
    ],
    "tags": []
  },
  {
    "id": 2,
    "name": "Jogos Mobile",
    "products": [
      {
        "id": 3,
        "name": "Clash of Clãs"
      },
      {
        "id": 4,
        "name": "Mobile Legends"
      }
    ],
    "tags": []
  },
  {
    "id": 4,
    "name": "PlayStation Store",
    "products": [
      {
        "id": 39,
        "name": "PSN Gift Card - R$ 30,00"
      },
      {
        "id": 40,
        "name": "PSN Gift Card - R$ 60,00"
      }
    ],
    "tags": []
  }
]';

I use the command $array = json_decode($json_string); to work with the following code:

$array = json_decode($json_string);
foreach ($array as $value)
    {
        for ($row = 0; $row < 5; $row++) {
        echo $value->products[($row)]->name .' - ID: '. $value->products[($row)]->id .'<br>';
    }
}

This returns me the names of the games as follows:

Grand Theft Auto V - ID: 34<br>
Bloodstained Ritual of the Night - ID: 59<br>
F1 2019 - Anniversary Edition - ID: 60<br>
PLAYERUNKNOWN'S BATTLEGROUNDS - ID: 62<br>
Batman: Arkham Knight - ID: 64<br>
Clash of Clãs - ID: 3<br>
Mobile Legends - ID: 4<br>

I used $row < 5 because I know that the largest amount of games in one of the lists is 5, so all appear, the problem is when the list has less than 5 games because PHP returns an error:

erro apresentado com o código atual

Therefore, considering that he did not know the amount of games in each of the sets, what would be the correct way to list all the games in the array?

  • It doesn’t have to be exactly with 'for', this was the closest way to get where I need to go, so I used the question.

2 answers

3


You can always use the count() to get the array size.


So if you’re using $value->products[($row)], can use the count($value->products) to get how many there are.

for ($row = 0; $row < count($value->products); $row++) {
    echo $value->products[($row)]->name .' - ID: '. $value->products[($row)]->id .'<br>';
}

You can also use the foreach, which was the same one you used before (you used foreach ($array as $value) before, so you know how this works).

foreach ($value->products as $product) {
    echo $product->name .' - ID: '. $product->id .'<br>';
}
  • worked perfectly, thank you for the reply

3

The value of the key products is also an array, as it is bounded by [ and ]:

"products": [
    ... etc
]

So just walk it with foreach also:

foreach ($array as $value) {
    foreach ($value->products as $product) {
        echo $product->name .' - ID: '. $product->id .'<br>';
    }
}

Exit:

Grand Theft Auto V - ID: 34<br>
Bloodstained Ritual of the Night - ID: 59<br>
F1 2019 - Anniversary Edition - ID: 60<br>
PLAYERUNKNOWN'S BATTLEGROUNDS - ID: 62<br>
Batman: Arkham Knight - ID: 64<br>
Clash of Clãs - ID: 3<br>
Mobile Legends - ID: 4<br>
PSN Gift Card - R$ 30,00 - ID: 39<br>
PSN Gift Card - R$ 60,00 - ID: 40<br>
  • 1

    had not thought to use one foreach inside the other, liked the alternative and also worked perfectly. thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.