如何在PHP中添加数字“1”作为电话号码的前缀?

I want to add the number one "1" in front of the phone numbers variable $toNumbers. I have tried the PHP string

$toNumbers = '1'.$toNumbers;

To append the 1 before the phone numbers but can't get it to work. What am I doing wrong here?

The below is from the vTiger SMS module and I need to format the number format with a 1 in front of it in order to send. I want to add the 1 in front of every number for the sms module to send properly. The issue is that all my phone numbers are stored as (xxx)xxx-xxxx instead of 1 (xxx)xxx-xxxx.

public function send($message, $toNumbers) {

    if(!is_array($toNumbers)) {
        $toNumbers = '1'.$toNumbers;
        $toNumbers = array($toNumbers);
    }


    $params = $this->prepareParameters();

    $params['text'] = $message;
    $params['to'] = implode(',', $toNumbers);


    $serviceURL = $this->getServiceURL(self::SERVICE_SEND);
    $httpClient = new Vtiger_Net_Client($serviceURL);
    $response = $httpClient->doPost($params);
    $responseLines = split("
", $response);

    $results = array();
    $i=0;
    foreach($responseLines as $responseLine) {
        $responseLine = trim($responseLine);
        if(empty($responseLine)) continue;

        $result = array( 'error' => false, 'statusmessage' => '' );
        if(preg_match("/ERR:(.*)/", trim($responseLine), $matches)) {
            $result['error'] = true;
            $result['to'] = $toNumbers[$i++];
            $result['statusmessage'] = $matches[0]; // Complete error message
        } else if(preg_match("/ID: ([^ ]+)TO:(.*)/", $responseLine, $matches)) {
            $result['id'] = trim($matches[1]);
            $result['to'] = trim($matches[2]);
            $result['status'] = self::MSG_STATUS_PROCESSING;
        } else if(preg_match("/ID: (.*)/", $responseLine, $matches)) {
            $result['id'] = trim($matches[1]);
            $result['to'] = $toNumbers[0];
            $result['status'] = self::MSG_STATUS_PROCESSING;
        } 
        $results[] = $result;
    } 
    return $results;
} 

You must handle the case when $toNumbers is an array, try this :

if(!is_array($toNumbers)) 
d{
    $toNumbers = '1'.$toNumbers;
    $toNumbers = array($toNumbers);
}
else
{
    foreach ($toNumbers as $key => $field) 
         $toNumbers[$key] = '1'.$toNumbers[$key];
}
if(!is_array($toNumbers)) {
    $toNumbers = '1'.$toNumbers;
    $toNumbers = array($toNumbers);
}

Is it even getting into this block of code? Is $toNumbers already an array? (You probably want to handle that case either way.)