2014-06-16 44 views
0

首先對不起我的英語 我有陣列將使用功能不能讓陣列的功能

  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); 
         } 
        } 
    

我的問題,如果我在文本文件中的數組 2個或多個元素將最後一個元素髮送消息 但如果我做陣列這樣

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

它的所有元素 工作,請幫助

+4

遺憾地說,這真的很難理解你想在這裏表達一下,在至少對我來說 – asprin

+1

你的'$ conn'數組與'SendBulk'函數有什麼關係? –

+0

我真的不明白。我現在可以告訴你的是,如果你想從一個文件中建立一個數組,每行都是一個數組元素,你可以使用PHP函數file()http://www.php.net/manual/en/ function.file.php。但我不知道你是否想要這個 – niconoe

回答

1

fgets()返回的字符串包含每行結束的換行符。使用rtrim()將其刪除:

$conn[] = rtrim($line); 

您也可以替換整個代碼讀取與文件:

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

非常感謝:D:D – erashdan