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

dinni's avatar
Level 1

Json encode null issue

Hi , I have a array which has empty string as value for an key (""), but when i json encode it its making that "" into a null value, how can i avoid this?

$data = [

"some_key"=>"",
"some_other_key"=>"xyz",
"timestamp"=>1601900562163

];

dd(json_encode($data));

Result {"some_key":null,"some_other_key":"xyz","timestamp":1601900562163}

Expected {"some_key":"","some_other_key":"xyz","timestamp":1601900562163}

0 likes
6 replies
a4ashraf's avatar

@dinni

My PHP Version is 7.3.15

and here is my output

$data = [
		"some_key"=>"",
		"some_other_key"=>"xyz",
		"timestamp"=>1601900562163
	];

	dd(json_encode($data));

"{"some_key":"","some_other_key":"xyz","timestamp":1601900562163}"

phpMick's avatar

Inside the PHPStorm Console:

‌‌json_encode($data)
‌{"some_key":"","some_other_key":"xyz","timestamp":1601900562163}

Please or to participate in this conversation.