选择将哪个变量插入数组中


Make a choice which variable gets inserted inside array

我有两个变量

$order_phone = $orders[$x]['phone'];
$order_mobile = $orders[$x]['mobile'];

使用 API 中的函数,我创建了一个新数组。在数组中,这些变量将像这样设置:

'telephone' => $order_phone . $order_mobile ,

现在,当长度超过20个字符时,我遇到了麻烦。 然后我得到这个错误:

Fatal error: Uncaught exception 'SendCloudApiException' with message 'telephone: "Ensure this field has no more than 20 characters."

我可以让插入的变量成为一种选择吗?如果填写了 2 中的 1 个,则选择该变量还是始终使用手机号码?

下面是创建的数组的其余部分:

$createdParcel = $apiSend->parcels->create(
    array(
            'name'=> ($order_firstName . $order_middleName . $order_lastName),
            'company_name' => $order_companyName,
            'address' => ($order_addressBillingStreet . $order_addressBillingStreet2 . $order_addressBillingNumber . $order_addressBillingExtension),
            'city' => $order_addressBillingCity,
            'postal_code' => $order_addressBillingZipcode,
            'telephone' => $order_phone . $order_mobile ,
            'requestShipment' => false, // set to true when you want to request a shipment
            'email' => $order_email,
            'country' => strtoupper($order_addressBillingCountry['code']),
            'order_number' => $order_number
          )
);

我真的不明白串联电话号码的用处,但我们开始了。

// If both numbers together have a maximum legth of 20 chars: use them both
if(strlen($order_phone . $order_mobile) <= 20) {
  $phone = $order_phone . $order_mobile;
} else {
  if(!empty($order_mobile)) {
    // if a mobile number is given use that
    $phone = $order_mobile;
  } else {
    // Use phone number other wise
    $phone = $order_phone;
  }
}
// at this point $phone is a string <= 20 chars
$createdParcel = $apiSend->parcels->create(
    array(
            'name'=> ($order_firstName . $order_middleName . $order_lastName),
            'company_name' => $order_companyName,
            'address' => ($order_addressBillingStreet . $order_addressBillingStreet2 . $order_addressBillingNumber . $order_addressBillingExtension),
            'city' => $order_addressBillingCity,
            'postal_code' => $order_addressBillingZipcode,
            'telephone' => $phone , // Use phone number from above
            'requestShipment' => false, // set to true when you want to request a shipment
            'email' => $order_email,
            'country' => strtoupper($order_addressBillingCountry['code']),
            'order_number' => $order_number
          )
);