Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php将数组中的空值转换为空值(";)_Php_Arrays_Laravel - Fatal编程技术网

Php将数组中的空值转换为空值(";)

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)

我将返回用户保存的所有配送地址,但有些值保留为空,这些值中的输出为空。如何将空值转换为空(“”)。我在互联网上寻找了许多解决方案,但无法使其发挥作用。 API链接:

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
                    });