繁体   English   中英

Zend PHP函数错误-警告:array_merge():参数#1不是以下数组

[英]Zend php function error - Warning: array_merge(): Argument #1 is not an array in

我有这个功能,并收到此错误

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

为什么我会收到此错误的任何提示?

您应该在传递给函数之前检查数组是否为空

尝试这个

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

连接到Twitter API时似乎没有进行身份验证。 如果未通过身份验证,则两个链接都会导致{"errors":[{"message":"Bad Authentication data","code":215}]} ,在这种情况下, $followers['ids']不会是一个数组,因为它不存在。

Twitter的API文档包含有关身份验证的信息。

如果不是这个问题,我深表歉意,但是从您的代码来看,这似乎很抱歉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM