【问题标题】:array_merge() [function.array-merge]: Argument #1 is not an arrayarray_merge() [function.array-merge]:参数 #1 不是数组
【发布时间】:2013-01-08 23:09:33
【问题描述】:

我正在尝试使用 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

我正在使用带有 PHP 5.3 的 XAMPP 的 Codeigniter 2.3

【问题讨论】:

    标签: codeigniter xampp php


    【解决方案1】:

    在你的函数中初始化你的数组(如果需要的话)

    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']();
      }
    

    【讨论】:

    • 它对我有用。谢谢。你能解释一下为什么 global $apiConfig = array();一开始没用?
    • 在将变量声明为全局变量时,您不能为其赋值。
    • 因为你试图在清除它的同时声明你想要全局。其次,这是您正在修改的 Google 代码。可能不是您想要的解决方案。您需要找出为什么代码没有加载在 config.php 中创建的 $apiConfig
    • 我猜是因为 Google 的 API 与 PHP 5.3 不兼容。更改后一切正常。
    • 您所做的只是绕过配置未加载的事实。 Google wiki 说要求是 PHP 5.3 或更高版本。
    【解决方案2】:

    检查您的服务器日志,看看是否有与 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();
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      相关资源
      最近更新 更多