为了账号安全,请及时绑定邮箱和手机立即绑定

从 API 输出空字符串

从 API 输出空字符串

PHP
呼唤远方 2023-04-02 10:51:53
因此,我使用以下数组传入$record params,但当 API 的特定返回为 null 时出现错误。这是数组:$office->load([    'post_title' => $record->office,    'location_id' => $record->location_id,    'location' => $record->location,    'business_unit' => $record->business_unit,    'type_id' => $record->type_id,    'brand_id' => $record->brand_id,]);我的$record->brand_id返回为 null 并且它使我的整个脚本崩溃,有没有一种方法可以将 null 输出为字符串并将 $record->brand_id 包装在某些东西中?更新:我最终配置了所需的东西brand_id,因为$record->brand_id ?? $default_value效果很好!
查看完整描述

1 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

如果您知道哪些值可能null对 API 输出没有影响,您可以尝试检查空值的运算符,例如null coalescing operator (??).


$default_value = 'default';


$office->load([

    'post_title' => $record->office,

    'location_id' => $record->location_id,

    'location' => $record->location,

    'business_unit' => $record->business_unit,

    'type_id' => $record->type_id,

    'brand_id' => $record->brand_id ?? $default,

]);


查看完整回答
反对 回复 2023-04-02
  • 1 回答
  • 0 关注
  • 79 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信