猿问

从 API 输出空字符串

因此,我使用以下数组传入$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效果很好!


呼唤远方
浏览 108回答 1
1回答

茅侃侃

如果您知道哪些值可能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,]);
随时随地看视频慕课网APP
我要回答