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

dk4210's avatar

Getting value from json_decode ( PHP )

Hello Guys,

I 'm trying to get this value, but having some issues

So I have a file called url.json and i'm loading it here $econfig = json_decode(file_get_contents("url.json"));

The code looks like this

"uris": {
		"DIT": "http://test1.com",
		"FIT": "http://test2.com",
		"UAT": "http://test3.com",
		"PROD": "http://test4.com",
		"PROD2": "http://test5.com"
	},
	"endpoints": {
		"create": "migrate",
		"status": "migrate"
	},
	"emails": [

	]
}

When I do var_dump($econfig) It outputs this

class stdClass#4 (3) {
  public $uris =>
  class stdClass#5 (5) {
    public $DIT =>
    string(42) "http://test1.com"
    public $FIT =>
    string(78) "http://test2.com"
    public $UAT =>
    string(85) "http://test3com"
    public $PROD =>
    string(81) "http://test4.com"
    public $PROD2 =>
    string(81) "http://test5.com/"
  }

How can I compare the env, for example "DIT" and get the value (Which in this case is http://test1.com)?

I know it will be something like $econfig>DIT;

Help is appreciated.

0 likes
4 replies
RamjithAp's avatar

When you do JSON decode the output will be an array format. So try this

$econfig = json_decode(file_get_contents("url.json"),TRUE);
return $econfig["uris"]["DIT"];
tykus's avatar
tykus
Best Answer
Level 104

As you can see form the dumped variable, the DIT property is nested under uris object, so you would use:

$econfig->uris->DIT

The laravel helper data_get would be useful in this context; in the event that part of the chain of properties does not exist, you would not get an error:

data_get($econfig, 'uris.DIT')

It has the advantage of working transparently for Objects and Arrays.

dk4210's avatar

Hi RamjithAp,

Thanks for the reply.

I updated the code.

$econfig = json_decode(file_get_contents("url.json", TRUE));

echo "This is the dit endpoint". $econfig["uris"]["DIT"];

I get the following error message

Cannot use object of type stdClass as array
dk4210's avatar

Tykus

That worked. Thank you!

Please or to participate in this conversation.