1

I'm trying to get the IDs of the steam groups someone is connected to. Here is the json output:

{
    "response": {
        "success": true,
        "groups": [
            {
                "gid": "111"
            },
            {
                "gid": "222"
            },
            {
                "gid": "333"
            },
            {
                "gid": "444"
            },
            {
                "gid": "555"
            }
        ]

    }
}

I've attempted it via:

$groupIDs = $reply['response']['groups'];
foreach ($groupIDs as $gID) {
    // Do stuff
}

I'm getting the following error, but I'm struggling to see how to correct it.

Invalid argument supplied for foreach()

Sorry I didn't make it clear. I'm already decoding it before the foreach().

    $reply = json_decode($reply, true);
0

4 Answers 4

1

First you have to decode the json string using php function json_decode. Then iterate the object as shown below

$string = '{
    "response": {
        "success": true,
        "groups": [
            {
                "gid": "111"
            },
            {
                "gid": "222"
            },
            {
                "gid": "333"
            },
            {
                "gid": "444"
            },
            {
                "gid": "555"
            }
        ]

    }
}';
$array = json_decode($string);

foreach($array->response->groups as $value ){
    echo $value->gid;
    echo "<br/>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

http://php.net/manual/pt_BR/function.json-decode.php

Try:

$response = json_decode($reply, true);
$groupIDs = $response['response']['groups'];
foreach ($groupIDs as $gID) {
    // Do stuff
}

1 Comment

What about "gid"?? $gID sounds like you have them already!
0

use json_decode($response) cf doc

$rep = json_decode($response)
foreach ($rep->response->groups as $gID) {
    // Do stuff
}

Comments

0
$groups = $reply['response']->groups;
foreach ($groups as $group) {
    print $group->gid;
}

In json each {} means is parsed as object, each [] is parsed as array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.