Php将数组中的空值转换为空值(";)
我将返回用户保存的所有配送地址,但有些值保留为空,这些值中的输出为空。如何将空值转换为空(“”)。我在互联网上寻找了许多解决方案,但无法使其发挥作用。 API链接:Php将数组中的空值转换为空值(";),php,arrays,laravel,Php,Arrays,Laravel,我将返回用户保存的所有配送地址,但有些值保留为空,这些值中的输出为空。如何将空值转换为空(“”)。我在互联网上寻找了许多解决方案,但无法使其发挥作用。 API链接: https://androidapp.factory2homes.com/api/shippings/3 您只需映射返回值,如下所示: return $address = DB::table('shippings') ->where('user_id' , $user_id)
https://androidapp.factory2homes.com/api/shippings/3
您只需映射返回值,如下所示:
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
});
也许你可以向我们展示你的“许多解决方案”,并告诉我们为什么它们不起作用?现在:让你的地址
成为一个真正有说服力的模型,例如模型配送
。通过简单地返回模型或返回模型资源,在响应中返回该模型。无论哪种方式,laravel都会在内部调用多个函数来获取属性值。您可以访问这些属性,将空值转换为空白。看见
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
});