在下面的代码片段中,已为在线表单创建了一个数据包,但如果有意将其中一个字段留空,则会引发错误。我该如何修改这个,如果字符串长度大于 0,则创建数据包,如果不是,就不要设置它或让它未定义?
$formData = $_POST['form'];//form post value
// Create vendor packet
$oVendor = new stdClass();
$oVendor->office_name = $formData['vendor_info'][1]['vendor_bus'];
$oVendor->first_name = $formData['vendor_info'][1]['vendor_first'];
$oVendor->middle_name = $formData['vendor_info'][1]['vendor_middle'];
$oVendor->last_name = $formData['vendor_info'][1]['vendor_last'];
$oVendor->business_address = array(
'street' => $formData['vendor_address'][1]['vendor_street'],
'city' => $formData['vendor_address'][1]['vendor_city'],
'state' => $formData['vendor_address'][1]['vendor_state'],
'zip' => $formData['vendor_address'][1]['vendor_zip'],
'is_primary' => $formData['vendor_address'][1]['checkbox_primary']);
$oVendor->mailing_address = array(
'street' => $formData['vendor_mail'][1]['vendor_street'],
'city' => $formData['vendor_mail'][1]['vendor_city'],
'state' => $formData['vendor_mail'][1]['vendor_state'],
'zip' => $formData['vendor_mail'][1]['vendor_zip'],
'is_primary' => $formData['vendor_mail'][1]['checkbox_primary']);
$emails = array();
$i = 1;
foreach($formData['vendor_emails'] as $email)
$emails[$i++] = $email['vendor_email'];
$oVendor->emails = $emails;