无法使数组生效

时间:2014-06-16 09:17:54

标签: php arrays function

首先抱歉我的英语 我有函数使用的数组

  1. 上传文本文件,每一行都是数组中的元素

    $fh = fopen("upload/".'1.txt','r');
    $conn = array();
    while ($line = fgets($fh)) {
        $conn[] = $line;
    }
    fclose($fh);
    $wbs->SendBulk($conn, "hello world");
    
  2. 转到功能

    public function SendBulk($targets, $message)
    {
        echo "Sending " . count($targets) . " bulk messages...<br />";
    
        foreach($targets as $target)
        {
            $this->wa->sendPresenceSubscription($target);
            $this->wa->pollMessages();
            $this->wa->sendMessageComposing($target);
            sleep(55);
            $this->wa->pollMessages();
            $this->wa->sendMessagePaused($target);
            static::$sendLock = true;
            echo "Sending message from " . $this->username . " to $target... ";
            $this->wa->sendMessage($target, $message); // Orginal
            while(static::$sendLock)
            {
                //wait for server receipt
                sleep(55);
            }
        }
    
  3. 我的问题如果我在文本文件中有2个或更多元素在数组中 将为最后一个元素发送一条消息 但如果我像这样做数组

    $conn= array("565684898", "484849815", "484897987", "515498798");
    

    它适用于所有元素 请帮忙

1 个答案:

答案 0 :(得分:1)

fgets()返回的字符串包含结束每一行的换行符。使用rtrim()将其删除:

$conn[] = rtrim($line);

您还可以用以下内容替换读取文件的整个代码:

$conn = file('upload/1.txt', FILE_IGNORE_NEW_LINES)