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

时间:2016-03-08 13:56:50

标签: php arrays

我有两个变量

$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
          )

);

1 个答案:

答案 0 :(得分:1)

我真的不明白连接电话号码的有用性,但我们走了。

// 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
          )

);