繁体   English   中英

array_merge()[function.array-merge]:参数#1不是数组

[英]array_merge() [function.array-merge]: Argument #1 is not an array

我正在尝试使用Google-API-PHP-Client,其基类会引发以下错误:

Severity: Warning

Message: array_merge() [function.array-merge]: Argument #1 is not an array

Filename: libraries/Google_Client.php

Line Number: 107

类似于107的代码类似于:

public function __construct($config = array()) {
    global $apiConfig;
    $apiConfig = array_merge($apiConfig, $config);
    self::$cache = new $apiConfig['cacheClass']();
    self::$auth = new $apiConfig['authClass']();
    self::$io = new $apiConfig['ioClass']();
  }

我知道global $apiConfig没有初始化为数组,这就是array_merge抛出错误的原因。 但当我将其更改为global $apiConfig = array(); ,得到另一个错误Parse error: syntax error, unexpected '=', expecting ',' or ';' in C:\\Softwares\\xampp\\htdocs\\testsaav\\application\\libraries\\Google_Client.php on line 106 Parse error: syntax error, unexpected '=', expecting ',' or ';' in C:\\Softwares\\xampp\\htdocs\\testsaav\\application\\libraries\\Google_Client.php on line 106

我正在使用Codeigniter 2.3和XAMPP,它有PHP 5.3

在函数中初始化数组(如有必要)

public function __construct($config = array()) {
    global $apiConfig;
    $apiConfig = (isset($apiConfig) && is_array($apiConfig)) ? $apiConfig : array(); // initialize if necessary
    $apiConfig = array_merge($apiConfig, $config);
    self::$cache = new $apiConfig['cacheClass']();
    self::$auth = new $apiConfig['authClass']();
    self::$io = new $apiConfig['ioClass']();
  }

检查服务器日志,看看是否存在与Google_Client.php中的require_once('config.php')相关的错误(如果找不到该文件,则该脚本应该已停止)。

当您执行require_once('Google_Client.php')时,将从该文件执行以下代码。 执行完需求后,您的脚本应该可以看到$ apiConfig。

// hack around with the include paths a bit so the library 'just works'
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());

require_once "config.php";
// If a local configuration file is found, merge it's values with the default configuration
if (file_exists(dirname(__FILE__)  . '/local_config.php')) {
  $defaultConfig = $apiConfig;
  require_once (dirname(__FILE__)  . '/local_config.php');
  $apiConfig = array_merge($defaultConfig, $apiConfig);
}

请注意,您不要触摸config.php。 如果需要覆盖其中的任何内容,则创建local_config.php。

从我的PHP 5.3系统中我使用了这个脚本。 如下所示的脚本不会引发任何错误。 取消设置$ apiConfig会复制您的错误。

<?php

require_once('src/Google_Client.php');

print_r($apiConfig);
// uncommenting the next line replicates issue.
//unset($apiConfig);
$api = new Google_Client();

?>

暂无
暂无

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

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