JDev
JDev

Reputation: 124

Error when trying to echo an array

I've got a problem. I can't find a proper array in this json file. When i try this:

<?php
$jsonurl = "http://steamcommunity.com/profiles/76561198132044757/inventory/json/304930/2";
$json = file_get_contents($jsonurl);

$arJson = json_decode( $json, true );
echo $arJson[0]
?>

it says:

Notice: Undefined offset: 0

or if i try this:

echo $arJson["rgInventory"]

it says:

Notice: Array to string conversion

Where are the arrays in my json file and how to adress them?

Thank you in advance and sorry for my bad english ;)

Jonas

Upvotes: 2

Views: 120

Answers (3)

swidmann
swidmann

Reputation: 2792

Notice: Undefined offset: 0

You get this error because your array is an associative array, that mean's there is no index 0. For more info take a look at the docs.

Notice: Array to string conversion

You get this error, because echo can only output strings not arrays

to see what's in your array, you can use var_dump()

<?php
$jsonurl = "http://steamcommunity.com/profiles/76561198132044757/inventory/json/304930/2";
$json = file_get_contents($jsonurl);

$arJson = json_decode( $json, true );
echo "<pre>";
var_dump( $arJson );

$arJson["rgInventory"] is also an array so you can see the values with:

var_dump( $arJson["rgInventory"] );

Upvotes: 3

rocky
rocky

Reputation: 631

you have to use print_r in order to print an array. See your result with

echo '<pre>';print_r($arJson); 

Upvotes: 2

murrometz
murrometz

Reputation: 904

1) First of all, you should read Steam API documentation to know the structure of coming data.
https://steamcommunity.com/dev

2) Use print_r and var_dump functions to see the structure of your variables.
For example <pre> <? print_r($arJson) ?>

Upvotes: 2

Related Questions