Skip to content
Advertisement

Php convert null value inside array to blank (” “)

I am returning all shipping address saved by user but there are some values which are left blank and output comes as null in those. How can i convert NULL values to blank (” “). I have looked many solutions on internet but not able to get make it working. API link:

https://androidapp.factory2homes.com/api/shippings/3
$address = DB::table('shippings')->where('user_id' , $user_id)->get();
  return $address;

Advertisement

Answer

You can just map the return value like:

return $address = DB::table('shippings')
                    ->where('user_id' , $user_id)
                    ->get()
                    ->map(function ($item) {
                       $mapped = [];

                       foreach ($item as $key => $value) {
                           $mapped[$key] = $value ?? ' ';
                       }
                       
                       return $mapped
                    });
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement