/    Sign up×
Community /Pin to ProfileBookmark

Why does var_dump return null?

I have 2 json objects that looks exactly the same but with different data. var_dumping one of them returns null while print_r works fine. Why is that so?
the other json-object works fine with both print_r and var_dump.
I think the problem is the data of one of the objects but i still dont get what the specific problem is

[CODE]
<?php

error_reporting(E_ALL);
ini_set(‘display_errors’, 1);

$url = “”;
$xml = simplexml_load_file($url);

$json = json_encode($xml);
$array = json_decode($json,true);

$obj = (object) [
‘data’ => [

],
];

$obj = json_encode($obj);
$obj = json_decode($obj, true);

$obj[‘data’][0] = [ (object) [
‘place’ => ”,
‘events’ => []]];

$obj = json_encode($obj);
$obj = json_decode($obj, true);

for($i=0; $i<$size; $i++){

………
gather the data
………

$obj[‘data’][0][0][‘events’][$i] = (object) [
‘starts’=> $starts,
‘ends’ => $ends,
‘info’ => $info
];
}

$obj = json_encode($obj);
var_dump(json_decode($obj, true));
[/CODE]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 11.2019 — You might not be able to use var_dump on a function? Just try dumping $obj alone. Or save the decode output into another var and then dump that.
Copy linkTweet thisAlerts:
@NogDogAug 12.2019 — Sounds like $obj is null due to the preceding

``<i>
</i>$obj = json_encode($obj);
$obj = json_decode($obj, true);<i>
</i>
`</CODE>
Why do you keep doing this? All it does is try to turn the array $obj into a JSON string, and then then immediate turn it back into an array.

The <C>
var_dump()</C> is probably saying "null" because for some reason at that point <C>$obj</C> is not a valid JSON string. so you need to do some dumping earlier to find out where it's not working.
<CODE>
`<i>
</i>23:48 $ php -a
Interactive shell

php &gt; $foo = "this is not JSON";
php &gt; var_dump(json_decode($foo, true));
NULL
php &gt; $bar = '{"test":"This is valid JSON"}';
php &gt; var_dump(json_decode($bar, true));
array(1) {
["test"]=&gt;
string(18) "This is valid JSON"
}
php &gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@bbq1authorAug 12.2019 — @NogDog#1607472 var_dump seems to work before the for-loop. I took a look at all the data in the objekt and found a �. Thats probably whats creating the problem, isnt it?
Copy linkTweet thisAlerts:
@NogDogAug 12.2019 — Possible...json_encode() might be choking on it. Maybe try adding the constant JSON_UNESCAPED_UNICODE as the 2nd parameter to json_encode()?
<i>
</i>$obj = json_encode($obj, JSON_UNESCAPED_UNICODE);
×

Success!

Help @bbq1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...