php - 数组中的 if 语句
问题描述
我有一个使用 cURL 发布的数据。现在我想知道我是否可以为 1 个数组传递 2 个变量。
这是我用来发送数据的代码
$data = [
'completion_date' => '31/03/2019',
'customer_id' => 80,
'full_name' => '',
'email_address' => 'email@email.com',
'telephone' => '012122212',
'mobile' => '0787878',
'address' => 'Line 1 address'.chr(10).'Line 2 address',
'city' => 'City',
'county' => 'County',
'postcode' => 'Postcode',
'site_company_name' => 'Site Company Name',
'site_full_name' => 'Site Contact Name',
'site_telephone' => '012121212',
'site_mobile' => '07878787',
'site_fax' => 'Depreciated, not in use',
'site_email_address' => 'email@email.com',
'site_address' => 'Site Line 1 address'.chr(10).'Line 2 address',
'site_city' => 'Site City',
'site_county' => 'Site County',
'site_postcode' => 'Site Postcode',
'site_notes' => 'Site Notes',
'customer_ref' => $RecordID,
'wo_ref' => 'Customer Job Ref',
'po_ref' => $OrderID,
'short_description' => 'short description of job',
'description' => 'long description of job',
'customer_notes' => 'Customer notes',
'job_products' => json_encode($job_products)
];
现在我想知道我是否可以做这样的事情
'customer_notes' => $variable1 or $variable2,
我试图完成的是,如果$variable1
是空的,则使用数据,$variable2
反之亦然。有没有办法做到这一点?
'full_name'=> if $var1 empty use $var2
解决方案
您可以使用三元运算符
'customer_notes' => !empty($variable1) ? $variable1 : $variable2,
推荐阅读
- excel - 从excel表格中提取数据和数量
- c++ - 线程中的 Condition_Signal(),它是如何工作的?
- javascript - 用js更新css动画名称,动画不起作用
- java - 使用 Android 的 Google 身份服务登录保存的帐户
- python - Python selenium 在 ebay 中找不到搜索框
- amazon-web-services - 代码部署后,来自控制台的更新后的 aws cloudformation 嵌套堆栈自定义参数获取其默认值
- php - 通过 Service-Repository 模式在数据库中创建新记录时如何正确处理异常。[拉拉维尔]
- sql - 存储过程需要参数“':' 附近的语法不正确。”
- ruby - 为什么我不小心在 Ruby 中创建了回文?
- python - Klepto(Python 模块)file_archive 是否应该比 Pickle 慢 10 倍?