1

I need to get a special entry from a json array and it should be matched by id.

{
            "response": {
                "count": 62,
                "inventory": [
                    {
                        "id": 10,
                        "style": 982
                    },
                    {
                        "id": 20,
                        "style": 0
                    },
                    {
                        "id": 30,
                        "style": 1
                    }       ]
    }}

Everything I have right now is this code:

$matching_value = 10;    
foreach($json_data as $key => $val){
    if($val->id == $matching_value){
      echo $val->id;
      echo $val->style;
       }
    }

But it is not working and I don't know why. Does my approach not work with sub-entries?

9
  • 1
    1) decode your json string 2) But it is not working What does that mean? Do you get an error? PHP warning code not working on line 3; Commented Oct 24, 2015 at 23:45
  • $json_data is the elements in "response", or the elements in "inventory"? Commented Oct 24, 2015 at 23:45
  • If you're return $key;ing before echo'ing, it will never echo... Is that what you were expecting? Commented Oct 24, 2015 at 23:46
  • 1
    I think you want foreach ($json_data['response']['inventory'] as $key => $val) { Commented Oct 24, 2015 at 23:47
  • Also, there is id in JSON, but there also are elements with appid and without id. This gonna fail as never before. Commented Oct 24, 2015 at 23:52

1 Answer 1

1

Here you go.

  • you need to decode the json string to an array
  • inside the foreach, you have used object property access syntax and i've changed that to array access ($val->id ... $val['id'])

Does my approach not work with sub-entries?

Not like you have it. You would have to test what you are iterating over, if its a string, int, array. Consider the nesting, etc. I have reassigned $inventory to make iterating over the inner array a bit easier, gets you closer to the value

Source:

// JSON string
$json = '{
    "response": {
        "count": 62,
        "inventory": [
            {
                "id": 10,
                "style": 982
            },
            {
                "id": 20,
                "style": 0
            },
            {
                "id": 30,
                "style": 1
            }
        ]
    }}
';

// debugging
var_dump($json, json_decode($json, true));

// decode json to array
$data = json_decode($json, true);

// prepare iteration

// reassign 
$inventory = $data['response']['inventory'];

$matching_value = 10;

foreach($inventory as $key => $val)
{
    //var_dump($val);

    if(isset($val['id']) && $val['id'] === $matching_value)
    {
      echo 'The ID: ' . $val['id'];
      echo 'The Style' . $val['style'];
    }
}

Output: The ID: 10The Style982

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your answer! Your code looks like the missing link to my code. But I get the json array from an url with file_get_contents and json_decode. If I use your code, I only get an internal error. Have you got a solution to this?
Well, probably an issue of setting the second parameter of json_decode to true. That decides if you get an array or an object, e.g. $jsonObject = json_decode(file_get_contents('source://json'));. Just change that to $jsonArray = json_decode(file_get_contents('source://json'), true); In short: add true as 2nd parameter to json_decode and then use the code above starting from // prepare iteration. | If that doesn't solve the issue, post the error message you get.
I have used your advices and code, but now I get the internal error 500 error, see my code: $contents = json_decode(file_get_contents($steam_api_url, true); $json_data_a = $contents['response']['games']; foreach($json_data_a as $key => $val) { //var_dump($val); if(isset($val['appid']) && $val['appid'] === $steam_game_id) { echo 'The AppID: ' . $val['appid']; echo 'The Time: ' . $val['playtime_forever']; } }
Hard to read. Hmm. json_decode is not properly closed with ) - $contents = json_decode(file_get_contents($steam_api_url, true)**)**; The rest of the code looks ok. You could enable PHPs error reporting or take look at the logs, what the internal error really is.
Ah yeah, I closed the bracket of the json_decode-command and now the error is gone. But now I don't have any output. I used var_dump to let me show, what I got from the url, but the foreach-loop does not provide any output, although var_dump gives back an object.
|

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.