-6

There is dynamic array that we received from database . It has some null value.

So I want to put empty string instead of null in value

I know, I can check with isset function. But it is dynamic array so it is difficult to find out number of key value pairs.

$hoteldetails = get_hotel_detail($hotel_id);
$response=json_encode($HotelDetail);

get Hotel details fetching from database. It may have some null value Ex- Latitude or longitude can have null. When I encode json_encode it display null.

I also tried array_filter but it is removing null value element. I do not want to remove key value element.

nitin jain
  • 298
  • 6
  • 19

1 Answers1

2

PHP Code (modified from Replacing empty string with nulls in array php):

$array = array(
   'first' => NULL,
   'second' => NULL
);

echo json_encode($array);

$array2 = array_map(function($value) {
   return $value === NULL ? "" : $value;
}, $array); // array_map should walk through $array

echo json_encode($array2);

Output:

{"first":null,"second":null}

{"first":"","second":""}
Community
  • 1
  • 1
Dr.Tricker
  • 553
  • 3
  • 19