php 警告:不能使用标量值作为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21524219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:13:46  来源:igfitidea点击:

Warning: Cannot use a scalar value as an array in

phparraysscalar

提问by Julian Martinez

I am seeing the following error when executing my status script:

我在执行我的状态脚本时看到以下错误:

Warning: Cannot use a scalar value as an array in 

$result[$array[$i*2]] = $array[$i*2+1];

What am I doing wrong? I have included the full code below:

我究竟做错了什么?我在下面包含了完整的代码:

<?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); 

The line I mentioned is causing the errors. I have no idea what I am doing wrong here...

我提到的那条线导致了错误。我不知道我在这里做错了什么......

回答by Havelock

You can try declaring the variable $result, as an array, before using it.

您可以尝试$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];
} 

回答by Davidson Campos

xdebug archive in session path c:\xammp\temp\xdebug___.txt with 2GB, delete and solve this, this work to me

会话路径 c:\xammp\temp\xdebug___.txt 中的 xdebug 存档,大小为 2GB,删除并解决此问题,这对我有用