加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-22 11:51:53 所属栏目:PHP 来源:互联网
导读:执行我的状态脚本时看到以下错误: 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 =

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

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 = "xffxffxffxffgetinfo"; 

// 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,odd 
for($i = 0; $i < count($array)/2; $i++) { 

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读