2013-06-18 45 views
1

陣列我有這個功能,我得到這個錯誤Zend的PHP函數的錯誤 - 警告:array_merge():參數#1不

Warning: array_merge(): Argument #1 is not an array in 

$diff = array_merge($followers['ids'], $friends['ids']); 

然後

Invalid argument supplied for foreach() in 

功能:

public function addtosystemAction(){ 
     $this->_tweeps = new Application_Model_Tweeps(); 
     $http = new Zend_Http_Client(); 
     $http->setUri('http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=testuser'); 
     $followers = Zend_Json::decode($http->request()->getBody(), true); 
     $http->setUri('http://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name=testuser'); 
     $friends = Zend_Json::decode($http->request()->getBody(), true); 
     $diff = array_merge($followers['ids'], $friends['ids']); 
     $resultArray = array(); 
     foreach ($diff as $id){ 
      if(FALSE == $this->_tweeps->checkExisting($id)){ 
       $resultArray[] = $id; 
       if(count($resultArray) == 50){ 
        break; 
       } 
      } 
    } 

任何提示,爲什麼我得到這個錯誤?

+4

您是否嘗試過調試之前是空的?使用'var_dump($ followers ['ids'])來檢查;'如果是數組或不是。 – Rikesh

回答

0

看起來你連接到Twitter API時沒有進行身份驗證。如果未通過身份驗證,則兩個鏈接的結果都是{"errors":[{"message":"Bad Authentication data","code":215}]},在這種情況下,$followers['ids']不會是數組,因爲它不存在。

Twitter's API documentation包含認證信息。

如果這不是問題,我很抱歉,但它看起來像你的代碼判斷。

1

,你應該檢查是否數組傳遞給函數

試試這個

public function addtosystemAction(){ 
    $this->_tweeps = new Application_Model_Tweeps(); 
    $http = new Zend_Http_Client(); 
    $http->setUri('http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=testuser'); 
    $followers = Zend_Json::decode($http->request()->getBody(), true); 
    $http->setUri('http://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name=testuser'); 
    $friends = Zend_Json::decode($http->request()->getBody(), true); 

    if((!empty($followers['ids'])) && (!empty($friends['ids']))){ 
     $diff = array_merge($followers['ids'], $friends['ids']); 
     $resultArray = array(); 
     if(!empty($diff)){ 
     foreach ($diff as $id){ 
     if(FALSE == $this->_tweeps->checkExisting($id)){ 
      $resultArray[] = $id; 
      if(count($resultArray) == 50){ 
       break; 
      } 
     } 
     } 
    } 
    } 
} 
相關問題