Laravel:转换任何具有空的数组的值
我的数组从数据库中获取:
public function get_orders($phone)
{
$customer = User::where('phone',$phone)->get();
$c_id = $customer[0]->id;
$orders = Order::where('cusID',$c_id)->orderBy('id','DESC')->get();
}
我获取此列表:
[
{
"id": 5375,
"cusID": 1015,
"websiteID": null,
"brandID": 8052
},
{
"id": 5378,
"cusID": 1015,
"websiteID": 1,
"brandID": null
}
现在如何将所有NULL值转换为一个空字符串一次?
我尝试了这个,但不工作
foreach ($orders as $order) {
foreach ($order as $key => $value) {
if (is_null($value)) {
$order[$key] = "";
}
}
return $orders;
}
,我也尝试过,但不能起作用:
array_map(function($v){
return ($v === null) ? "" : $v;
}, $order->toArray());
I have array gets from database :
public function get_orders($phone)
{
$customer = User::where('phone',$phone)->get();
$c_id = $customer[0]->id;
$orders = Order::where('cusID',$c_id)->orderBy('id','DESC')->get();
}
I get this list :
[
{
"id": 5375,
"cusID": 1015,
"websiteID": null,
"brandID": 8052
},
{
"id": 5378,
"cusID": 1015,
"websiteID": 1,
"brandID": null
}
Now how can I convert all null values to empty string once?
I tried this but dont work
foreach ($orders as $order) {
foreach ($order as $key => $value) {
if (is_null($value)) {
$order[$key] = "";
}
}
return $orders;
}
also I tried this but not works:
array_map(function($v){
return ($v === null) ? "" : $v;
}, $order->toArray());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过变更顺序到达数组解决了它:
I Solved it by change order to Array: