json decode to array

0 votes

I want to decode a JSON string into an array but I am getting this error.

Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\temp\asklaila.php on line 6

Here is the code:

<?php
$json_string = 'http://www.domain.com/jsondata.json';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);
?>

Can someone help me fix this?

Jun 3, 2022 in PHP by Kichu
• 19,040 points
1,817 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

To do this, you have to specify true as the second argument if you want an associative array instead of an object from json_decode. 
For that use this code:

$result = json_decode($jsondata, true);

To have integer keys instead of whatever the property names are:

$result = array_values(json_decode($jsondata, true));

In your case just access it as an object:

print_r($obj->Result);

I hope this helps you.

answered Jun 4, 2022 by narikkadan
• 86,360 points

edited Mar 5, 2025

Related Questions In PHP

0 votes
1 answer

How to get xdebug var_dump to show full object/array?

Hello. These are configurable variables in php.ini: ; with ...READ MORE

answered Apr 7, 2020 in PHP by Niroj
• 82,800 points
4,548 views
0 votes
1 answer

How to pass an array within a query string?

Hello, Submitting multi-value form fields, i.e. submitting arrays ...READ MORE

answered Apr 8, 2020 in PHP by Niroj
• 82,800 points
13,385 views
0 votes
1 answer

How to convert JSON string to array?

Hello @kartik, If you pass the JSON in ...READ MORE

answered Aug 27, 2020 in PHP by Niroj
• 82,800 points
1,527 views
–1 vote
1 answer
0 votes
1 answer

How to store values from foreach loop into an array?

sfsf gf fgfgf fhfh READ MORE

answered Nov 10, 2023 in PHP by anonymous

edited Mar 5, 2025 31,368 views
0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,800 points
3,322 views
0 votes
0 answers

PHP Array to JSON Array using json_encode();

I have encoded an Array I've made using ...READ MORE

Jun 13, 2022 in PHP by narikkadan
• 86,360 points
1,249 views
0 votes
0 answers

jQuery JSON Decode ( PHP to Javascript)

I'm trying to make an autocomplete script. ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,580 points
982 views
0 votes
1 answer

Cant get a specific value in JSON/PHP

Convert the json-string to a php-array and ...READ MORE

answered Sep 11, 2018 in Blockchain by slayer
• 29,370 points
3,505 views
0 votes
1 answer

jsonRPCClient Error with php variables

All you have to do is change ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
2,681 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP