为什么只有一个服务器抛出错误“数组到字符串转换”


Why is only one server throwing the error "Array to String Conversion"?

以下是导致错误的代码:

        foreach($_GET['Inventory'] as $fld => $val) {
           print_r("Field: " . $fld . " Value: " . $val . '</br>');
            if($val != '' && !is_null($val))
                if($fld != 'searchoption')
                    $perma .=  $fld . '=' . $val . '&';
        } 

print_r() 是错误的原因,但我只在一台服务器上收到此错误。该服务器最近经历了Ubuntu(升级到14.04全新安装)和LAMPP的全新安装。这与其他正在运行的源代码相同(据我所知),但由于某种原因,此错误在这里持续存在。

我不熟悉php-apache模块,我无法摆脱系统缺少可以解决此问题的核心组件的感觉。

编辑(更多信息):如果$val中确实存在数组,则服务器上的一个不是抛出错误,而是在数组的位置显示"数组"。我不想更改代码,而是找出可能导致这种情况的原因。

执行搜索后工作服务器的输出:

Field: searchoption Value: Array
Field: parentBarcode Value:
Field: barcode Value:
Field: room Value:
Field: fixedAssetTag Value:
Field: hostDomainName Value:
Field: ipAddress Value: test
Field: macAddress Value:
Field: serialNumber Value:
Field: purchaseOrder Value:
Field: accountNumber Value:
Field: searchscope Value: 1

编辑:两台服务器上的源代码完全相同。

在无法正常工作的服务器上加载了 Apache 模块:

//both servers have these modules loaded
=   core_module (static)                        
=   so_module (static)      
=   http_module (static)
=   log_config_module (static)
=   logio_module (static)
=   alias_module (shared)
=   auth_basic_module (shared)
=   authn_file_module (shared)
=   authz_host_module (shared)
=   authz_user_module (shared)
=   autoindex_module (shared)
=   cgi_module (shared)
=   deflate_module (shared)
=   dir_module (shared)
=   env_module (shared)
=   mime_module (shared)
=   mpm_prefork_module (shared)
=   negotiation_module (shared)
=   php5_module (shared)
=   setenvif_module (shared)
=   status_module (shared)
//Server with error has these extra modules loaded
+   authz_core_module (shared)
+   authn_core_module (shared)
+   version_module (static)
+   unixd_module (static)
+   access_compat_module (shared)
+   rewrite_module (shared)     
+   watchdog_module (static)
+   filter_module (shared)
//Working server has these extra modules loaded
- authz_groupfile_modeul (shared)
- authz_default_module (shared)
- reqtimeout_module (shared)

两个服务器上都可能存在此错误:您正在尝试将数组输出为字符串。这是您应该解决的问题。您可能只在一台服务器上看到错误消息,因为此服务器对 PHP 配置变量 error_reporting 具有更严格的设置。

我建议将记录器行分成这样的东西:

print_r($fld);
print_r($val);

无论变量类型如何,它都将起作用。

虽然这并不能回答问题本身,但我想提出一个更好的方法。

看起来您的代码正在尝试获取$_GET['Inventory']的内容并从中创建查询字符串,忽略所有空白条目以及键searchoption

试试这个代码:

$inventory = $_GET['Inventory'];
$inventory = array_filter($inventory); // remove blanks
if( isset($inventory['searchoption'])) unset($inventory['searchoption']);
$result = http_build_query($inventory);

特别是,这将很好地处理数组。

正如 Tim Fountain 所说,其中一个服务器对 error_reporting 变量进行了更严格的设置。

发生的事情是,我重新安装了 Ubuntu 14.04,并安装了所有最新的软件版本,包括 PHP 5.5。另一台服务器运行 PHP 5.3。事实证明,PHP 5.5 默认具有更严格的error_reporting设置(请参阅此处)。这回答了为什么错误在一台服务器上而不是另一台服务器上抛出。

有几种方法可以更改此设置;一种是更改php中的error_repoting选项设置.ini(对我来说是/etc/php5/apache2/php.ini)。我最终做的是,如果变量是数组,我将代码更改为打印"Array",而不是尝试连接它,这就是导致错误被抛出的原因。