Php 警告:无法在中使用标量值作为数组
我在执行状态脚本时看到以下错误:Php 警告:无法在中使用标量值作为数组,php,arrays,scalar,Php,Arrays,Scalar,我在执行状态脚本时看到以下错误: Warning: Cannot use a scalar value as an array in $result[$array[$i*2]] = $array[$i*2+1]; 我做错了什么? 我已将完整代码包括在下面: <?php // Set the host and port $host = "ip_goes_here"; $port = port_goes_here; // Open the socket connection
Warning: Cannot use a scalar value as an array in
$result[$array[$i*2]] = $array[$i*2+1];
我做错了什么?
我已将完整代码包括在下面:
<?php
// Set the host and port
$host = "ip_goes_here";
$port = port_goes_here;
// Open the socket connection to the server
$fp = fsockopen("udp://".$host, $port);
// Set the string to send to the server
$string = "\xff\xff\xff\xffgetinfo";
// Set the socket timeout to 2 seconds
socket_set_timeout($fp, 2);
// Actually send the string
fwrite($fp, $string);
// Read the first 18 bytes to get rid of the header and do the error checking here
if(!fread($fp, 18)) {
die("Oh God, the pain!");
}
// Get the status of the socket, to be used for the length left
$status = socket_get_status($fp);
// Read the rest
$info = fread($fp, $status['unread_bytes']);
// Explode the result of the fread into another variable
$array = explode("\\", $info);
// Loop through and create a result array, with the key being even, the result, odd
for($i = 0; $i < count($array)/2; $i++) {
$result[$array[$i*2]] = $array[$i*2+1];
}
// Print the result for error checking
print_r($result);
// Close the file pointer
fclose($fp);
在使用变量之前,可以尝试将变量$result
声明为数组
$result = array();
// Loop through and create a result array, with the key being even, the result, odd
for($i = 0; $i < count($array)/2; $i++) {
$result[$array[$i*2]] = $array[$i*2+1];
}
$result=array();
//循环并创建一个结果数组,其中键为偶数,结果为奇数
对于($i=0;$i
会话路径c:\xammp\temp\xdebug_u__;.txt中的xdebug存档,使用2GB,删除并解决此问题,这项工作对我来说很明显$array
不是数组,即使变量命名为数组也是如此。也可能是您已经将$result
设置为标量。。。如果不显示代码,在哪里设置这些变量,没有人能告诉你哪里出了问题。请提供完整的代码-现在还不清楚哪里出了问题对不起。我已经添加了完整的代码。这在PHP中不需要(但会生成一个E_通知,这是一个糟糕的做法)。问题涉及到运行时类型定义错误,你的解决方案没有提供一个明确的关系。你应该试着让xdebug在php的会话文件夹中创建这个巨大的文件,看看这个错误是否发生过,然后再进行负面响应,我并没有说这是这个问题的唯一原因,我只是说这发生在我身上,采用这个解决方案对我来说是有效的。。。