Harsha M V
Harsha M V

Reputation: 54969

Use json_decode() to create array insead of an object

I am trying to decode a JSON string into an array but i get the following error.

Fatal error: Cannot use object of type stdClass as array

Here is the code:

$json_string = 'http://www.example.com/jsondata.json';

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

Upvotes: 498

Views: 862288

Answers (12)

Stephen
Stephen

Reputation: 18907

As per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code:

$result = json_decode($jsondata, true);

If you want integer keys instead of whatever the property names are:

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

However, with your current decode you just access it as an object:

print_r($obj->Result);

Upvotes: 983

Solomon Suraj
Solomon Suraj

Reputation: 1324

I hope this will help you

$json_ps = '{"courseList":[  
            {"course":"1", "course_data1":"Computer Systems(Networks)"},  
            {"course":"2", "course_data2":"Audio and Music Technology"},  
            {"course":"3", "course_data3":"MBA Digital Marketing"}  
        ]}';

Use Json decode function

$json_pss = json_decode($json_ps, true);

Looping over JSON array in php

foreach($json_pss['courseList'] as $pss_json)
{

    echo '<br>' .$course_data1 = $pss_json['course_data1']; exit; 

}

Result: Computer Systems(Networks)

Upvotes: 4

Shanu Singh
Shanu Singh

Reputation: 101

json_decode($data, true); // Returns data in array format 

json_decode($data); // Returns collections 

So, If want an array than you can pass the second argument as 'true' in json_decode function.

Upvotes: 5

Arosha De Silva
Arosha De Silva

Reputation: 597

According to the PHP Documentation json_decode function has a parameter named assoc which convert the returned objects into associative arrays

 mixed json_decode ( string $json [, bool $assoc = FALSE ] )

Since assoc parameter is FALSE by default, You have to set this value to TRUE in order to retrieve an array.

Examine the below code for an example implication:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));

which outputs:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

Upvotes: 6

xkeshav
xkeshav

Reputation: 54050

try this

$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo "<pre>";
print_r($obj);

Upvotes: 50

Arjun Kariyadan
Arjun Kariyadan

Reputation: 499

json_decode support second argument, when it set to TRUE it will return an Array instead of stdClass Object. Check the Manual page of json_decode function to see all the supported arguments and its details.

For example try this:

$json_string = 'http://www.example.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, TRUE); // Set second argument as TRUE
print_r($obj['Result']); // Now this will works!

Upvotes: 4

lalithkumar
lalithkumar

Reputation: 3540

Try like this:

$json_string = 'https://example.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj->Result);
foreach($obj->Result as $value){
  echo $value->id; //change accordingly
}

Upvotes: 2

Please try this

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

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
echo "<pre>"; print_r($obj['Result']);
?>

Upvotes: 3

Salman Mohammad
Salman Mohammad

Reputation: 192

in PHP json_decode convert json data into PHP associated array
For Ex: $php-array= json_decode($json-data, true); print_r($php-array);

Upvotes: 2

designosis
designosis

Reputation: 5263

This is a late contribution, but there is a valid case for casting json_decode with (array).
Consider the following:

$jsondata = '';
$arr = json_decode($jsondata, true);
foreach ($arr as $k=>$v){
    echo $v; // etc.
}

If $jsondata is ever returned as an empty string (as in my experience it often is), json_decode will return NULL, resulting in the error Warning: Invalid argument supplied for foreach() on line 3. You could add a line of if/then code or a ternary operator, but IMO it's cleaner to simply change line 2 to ...

$arr = (array) json_decode($jsondata,true);

... unless you are json_decodeing millions of large arrays at once, in which case as @TCB13 points out, performance could be negatively effected.

Upvotes: 31

coreyavis
coreyavis

Reputation: 97

This will also change it into an array:

<?php
    print_r((array) json_decode($object));
?>

Upvotes: 5

Anuj Pandey
Anuj Pandey

Reputation: 938

Just in case you are working on php less then 5.2 you can use this resourse.

http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/

http://mike.teczno.com/JSON/JSON.phps

Upvotes: 6

Related Questions