Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Robinvm's avatar

Make a foreach with a JSON array

Hello, i want to make a foreach with info from JSON, but i cant get it to work.

This is the JSON {"extractorData":{"url":"http://www.gametracker.com/search/minecraft/?","resourceId":"ef620045b25dd59aaf787ed293b77a72","data":[{"group":[{"ip":[{"text":"188.212.101.184"}],"port":[{"text":":25565"}]},{"ip":[{"text":"37.187.143.232"}],"port":[{"text":":25565"}]},{"ip":[{"text":"37.187.155.150"}],"port":[{"text":":25565"}]},{"ip":[{"text":"149.202.88.8"}],"port":[{"text":":25565"}]},{"ip":[{"text":"178.33.168.228"}],"port":[{"text":":25565"}]},{"ip":[{"text":"151.80.111.140"}],"port":[{"text":":25565"}]},{"ip":[{"text":"37.221.210.41"}],"port":[{"text":":25565"}]},{"ip":[{"text":"136.243.174.135"}],"port":[{"text":":25565"}]},{"ip":[{"text":"91.121.101.49"}],"port":[{"text":":25565"}]},{"ip":[{"text":"92.222.126.77"}],"port":[{"text":":25565"}]},{"ip":[{"text":"149.56.28.188"}],"port":[{"text":":25565"}]},{"ip":[{"text":"188.212.102.151"}],"port":[{"text":":30002"}]},{"ip":[{"text":"158.69.23.185"}],"port":[{"text":":25565"}]},{"ip":[{"text":"149.202.65.52"}],"port":[{"text":":25565"}]},{"ip":[{"text":"37.143.163.10"}],"port":[{"text":":25565"}]}]}]},"pageData":{"resourceId":"ef620045b25dd59aaf787ed293b77a72","statusCode":200,"timestamp":1487618249143},"url":"http://www.gametracker.com/search/minecraft/?","runtimeConfigId":"ac291588-acfe-4d3a-9b48-1f770166ca96","timestamp":1487618249718,"sequenceNumber":-1}

And this is what i got now:

$area = json_decode($string, true);

        foreach($area['port'] as $i => $v)
        {
            echo $v['port'].'<br/>';
        }

Could someone help me to get this to work?

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

Have you looked at the structure of the JSON,

[
     "extractorData" => [ // level 0
       "url" => "http://www.gametracker.com/search/minecraft/?",
       "resourceId" => "ef620045b25dd59aaf787ed293b77a72",
       "data" => [ //level 1
         [ //level 3
           "group" => [ // level 4
             [ //level 5
               "ip" => [
                 [
                   "text" => "188.212.101.184",
                 ],
               ],
               "port" => [ //level 6
                 [ //level 7
                   "text" => ":25565",
                 ],
               ],
             ],

The port key is a child of extractorData, data, 0, group, port, 0 so your loop should be:

foreach($area['extractorData']['data'][0]['group'] as $group) {
    echo $group['port'][0]['text'])
}
1 like
cracker182's avatar

Hi,

i think your iteration is wrong.

$area contains the following array:

$area => [
    url => "http://www.gametracker.com/search/minecraft/",
    resourceId => "ef620045b25dd59aaf787ed293b77a72",
    data => [
        0 => [
            group => [
                0 => [
                    ip => [
                        text => "188.212.101.184"
                    ]
                ]
            ]
        ]
    ]
]

You need to reformat your json or iterate through $area["data"]["group"]

Greetings Dennis

1 like

Please or to participate in this conversation.