在下面的代码段中,已为在线表单创建了一个数据包,但如果其中一个字段有意留空,则会引发错误。我如何修改这个,以便说如果字符串长度大于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;