-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.

0

1 Answer 1

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":""}
Sign up to request clarification or add additional context in comments.

1 Comment

It is removing key. I do not want to remove key

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.