0

I'm working with the JSON here: http://steamcommunity.com/id/mitch8910/inventory/json/730/2/

I'm trying to get the tags[{"name":""}] part of it. For example, I would want the 'container', from '"name":"Container"'

This is my code here:

    $data = file_get_contents('http://steamcommunity.com/id/mitch8910/inventory/json/730/2/');
    $json = json_decode($data);

            foreach ($json->rgDescriptions as $mydata)
    {
         echo $mydata->tags[1];
    }

tags[] is an array, though I did tags[1], I don't awlays want the '1'th element because the position of "name" could change in the array for different elements, I just did this to test, but I got an error. I have tried multiple ways with multiple errors, that why I'm not posting all the error code.

1

2 Answers 2

1

The simplest way may be to do an extra loop to grab all the name elements:

$data = file_get_contents('http://steamcommunity.com/id/mitch8910/inventory/json/730/2/');

$json = json_decode($data);

foreach ($json->rgDescriptions as $mydata)
{
    foreach($mydata->tags as $tag) {
        echo $tag->name;
    }
}

or to get the first name of each tag group:

foreach ($json->rgDescriptions as $mydata)
{
    echo $mydata->tags[0]->name;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that works. Though, in each of the tag[] arrays, there are a few groups in curly brackets {} and each group has a "name" element in it. Do you think it would be possible to just get the "name" from the first set of curly brackets?
$mydata->tags[0]->name; will just get the first one, without needing the second loop
1

I updated the whole answer:

$json = json_decode($data, true);
if (isset($json['rgDescriptions']) && is_array($json['rgDescriptions'])){
    foreach ($json['rgDescriptions'] as $array_no => $value) {
        if (isset($json['rgDescriptions'][$array_no]['tags'])){
            echo "{$array_no}::::";
            foreach ($json['rgDescriptions'][$array_no]['tags'] as $k => $value) {
                if (isset($json['rgDescriptions'][$array_no]['tags'][$k]['name'])){
                    echo "{$json['rgDescriptions'][$array_no]['tags'][$k]['name']},";
                }
            }
        echo "<br />";

        }
    }
}

3 Comments

What does this do? All it seems to do is no longer allow me to go through the elements with my foreach, which I need to do, and I still can't get the "name" element.
I updated the answer. Now it checks if rgDescriptions is set and is an array. If yes it goes deeper through each contained array, and check if it has [name] tag inside the second row of arrays. If it is, then it prints it.
Thanks, this did work properly. Though I just need the first 'name' element in the tags array because there were a few for each item. I'm sure you could have done this, John C just gave me an answer on how to do that though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.