PHP 未定义偏移量:1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18304085/
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 17:18:51  来源:igfitidea点击:

PHP Undefined offset: 1

phplaravelpreg-matchlaravel-4

提问by erm_durr

Here's my PHP script:

这是我的 PHP 脚本:

    <?php
$address = htmlentities(strtolower(Config::get('aac.ip')));
$port = Config::get('aac.port');

@$sock = fsockopen ($address, $port, $errno, $errstr, 1);
if(!$sock) echo '<small class="muted"><strong>Server status:</strong></small> <span class="label label-important">OFFLINE</span><br />'; else {
$info = chr(6).chr(0).chr(255).chr(255).'info';
fwrite($sock, $info);
$data='';
while (!feof($sock))$data .= fgets($sock, 1024);
fclose($sock);

// if needed
// var_dump($data);


preg_match('/players online="(\d+)" max="(\d+)"/', $data, $matches);
$players = ''.$matches[1].' / '.$matches[2];
preg_match('/uptime="(\d+)"/', $data, $matches);
$h = floor($matches[1] / 3600);
$m = floor(($matches[1] - $h*3600) / 60);
$uptime = ''.$h.'h '.$m.'m';
preg_match('/monsters total="(\d+)"/', $data, $matches); 
$monsters = ''.$matches[1]; 
preg_match('#<motd>(.*?)</motd>#s', $data, $matches); 
$motd = ''.$matches[1];
preg_match('/npcs total="(\d+)"/', $data, $matches);
$npcs = ''.$matches[1];

if(empty($players) or $players == " / ") $players = "??? / ???";
if(empty($uptime)) $uptime = "???";
if(empty($monsters)) $monsters = "???";
if(empty($motd)) $motd = "???"; 

echo "
<small class='muted'><strong>Server status:</strong></small> <span class='label label-success'>ONLINE</span><br />
<small class='muted'><strong>Players:</strong> $players</small><br />
<small class='muted'><strong>Uptime:</strong> $uptime</small><br />
<small class='muted'><strong>Monsters:</strong> $monsters</small><br />
<small class='muted'><strong>MOTD:</strong> $motd</small><br />
$npcs
";
}

?>

The $npcsvariable returns an error Undefined offset: 1. I tried to solve it many ways, but none of them worked. Also, here's the var_dump of data:

所述$npcs可变返回一个错误Undefined offset: 1。我尝试了很多方法来解决它,但都没有奏效。此外,这里是数据的 var_dump:

    string '<?xml version="1.0"?>
<tsqp version="1.0"><serverinfo uptime="33360" ip="127.0.0.1" servername="Forgotten" port="7171" location="Europe" url="http://otland.net/" server="The Forgotten Server" version="0.2.15" client="9.86"/><owner name="" email="@otland.net"/><players online="2" max="1000" peak="2"/><monsters total="636"/><map name="forgotten" author="Komic" width="1000" height="1000"/><motd>Welcome to the Forgotten Server!</motd></tsqp>
' (length=442)

I'm trying to retrieve the info. Also, am I able to retrieve the servername, location, and things like that? I'm not experienced very much. :P

我正在尝试检索信息。另外,我能否检索服务器名称、位置等信息?我不是很有经验。:P

回答by Neil Girardi

An undefined offset error in PHP happens when you try to access an array item using an index that does not exist for example, let's say I have the following:

例如,当您尝试使用不存在的索引访问数组项时,会发生 PHP 中的未定义偏移错误,假设我有以下内容:

<?php
$fruit = array('apple', 'orange', 'pear');
echo $fruit[0]; // apple
echo $fruit[1]; // orange
echo $fruit[3]; // Undefined offset:3 

In the above example the array has three items. The indexes to those items are 0,1,2. There is no item with an index of 3. Hence the error when I attempt to access $fruit[3].

在上面的例子中,数组有三个项目。这些项目的索引是 0,1,2。没有索引为 3 的项目。因此,当我尝试访问 $fruit[3] 时出现错误。

In your specific case there is no $matches[1]. You should troubleshoot the preg_match() call and see if your pattern is returning any matches.

在您的特定情况下,没有 $matches[1]。您应该对 preg_match() 调用进行故障排除并查看您的模式是否返回任何匹配项。

As a more general piece of advice, I suggest you check for the existence of an array key before attempting to access it.

作为更一般的建议,我建议您在尝试访问数组键之前检查它是否存在。