php-注意:未定义的偏移量:0 in
我收到此PHP错误,这是什么意思?
Notice: Undefined offset: 0 in
C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
从此代码:
include("config.php");
function getAllVotes($id)
{
$votes = array();
$q = "SELECT * FROM entries WHERE id = $id";
$r = mysql_query($q);
if(mysql_num_rows($r)==1)//id found in the table
{
$row = mysql_fetch_assoc($r);
$votes[0] = $row['votes_up'];
$votes[1] = $row['votes_down'];
}
return $votes;
}
function getEffectiveVotes($id)
{
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1]; //ERROR THROWN HERE
return $effectiveVote;
}
$id = $_POST['id'];
$action = $_POST['action'];
//get the current votes
$cur_votes = getAllVotes($id);
//ok, now update the votes
if($action=='vote_up') //voting up
{
$votes_up = $cur_votes[0]+1; //AND ERROR THROWN HERE
$q = "UPDATE threads SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down')
{
$votes_down = $cur_votes[1]+1;
$q = "UPDATE threads SET votes_down = $votes_down WHERE id = $id";
}
$r = mysql_query($q);
if($r)
{
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote." votes";
}
elseif(!$r) //voting failed
{
echo "Failed!";
}
?>
louismoore18 asked 2019-11-18T00:34:27Z
13个解决方案
78 votes
您要在0的键$votes处获取值。它是一个不包含该键的数组。
未设置数组$votes,因此当PHP尝试访问数组的键0时,它将遇到[0]和[1]的未定义偏移,并引发错误。
如果有数组:
$votes = array('1','2','3');
现在,我们可以访问:
$votes[0];
$votes[1];
$votes[2];
如果我们尝试访问:
$votes[3];
我们将收到错误“通知:未定义的偏移量:3”
YonoRan answered 2019-11-18T00:35:11Z
8 votes
这个答案帮助了我[https://stackoverflow.com/a/18880670/1821607]暗恋的原因-未设置索引0。 简单的$array = $array + array(null)可以解决问题。 或者,您应该检查是否通过isset($array[0])设置了索引0上的数组元素。第二种方法对我来说是最好的方法。
Dmitrii Malyshev answered 2019-11-18T00:35:36Z
7 votes
使用print_r($votes);检查阵列$votes,您将看到其中不存在键0。 它将返回NULL并抛出该错误。
Giulio Prisco answered 2019-11-18T00:36:01Z
4 votes
var_dump()未返回索引为0或1的数组,请通过对结果调用var_dump()来确保其返回了所需的数据。
Tim Cooper answered 2019-11-18T00:36:27Z
3 votes
首先,检查数组是否确实存在,您可以尝试类似
if (isset($votes)) {
// Do bad things to the votes array
}
Agg-rey Muhebwa answered 2019-11-18T00:36:52Z
2 votes
如前所述,这是因为"$votes_up = $cur_votes[0]+1;"中没有数据,因此会引发错误。
为确保您的代码正常工作,在执行"$votes_up = $cur_votes[0]+1;"之前,应回显$cur_votes[0]值,以查看是否存储了任何值。
当然,没有存储任何值。
Espanta answered 2019-11-18T00:37:31Z
1 votes
function getEffectiveVotes($id)
根据功能标头,只有一个参数变量($id)。因此,在第27行上,getEffectiveVotes()数组是未定义的并且超出范围。 您需要添加另一个参数值添加到函数头,以便函数getEffectiveVotes()知道需要两个参数。 我很生锈,但是这样的工作还是可以的。
function getEffectiveVotes($id, $votes)
我并不是说这应该怎么做,但是您可能想研究PHP传递其数组并确定是否需要显式声明以通过引用传递它
function getEffectiveVotes($id &$votes)
最后,调用函数getEffectiveVotes(),并在两个参数都应调用的地方调用它。
干杯。
Anthony Rutledge answered 2019-11-18T00:38:13Z
1 votes
您可能已经知道错误了。 这是由于尝试访问空数组或试图访问数组的空键的值。 在我的项目中,我正在通过计数数组和显示结果来处理此错误。
你可以这样做:
if(count($votes) == '0'){
echo 'Sorry, no votes are available at the moment.';
}
else{
//do the stuff with votes
}
count($votes)包含$votes阵列。 如果它等于零(0),则可以显示您的自定义消息或重定向到某些页面,否则您可以使用$votes进行处理。这样,您可以在PHP通知中删除Notice: Undefined offset: 0。
vijayrana answered 2019-11-18T00:38:53Z
0 votes
如果省略括号,则PHP将默认分配密钥。
试试这个:
$votes = $row['votes_up'];
$votes = $row['votes_down'];
David answered 2019-11-18T00:39:25Z
0 votes
我也遇到了这种情况,解决方案很简单,不要在代码中对数组索引位置进行硬编码。
代替$data[0]['somekey']做foreach($data as $data_item)
{
echo $data_item['somekey'];
}
如果有一个或多个数组,则可以在循环内执行所需的操作,但是如果未定义,则不会带来错误。 您还可以在此之上添加其他检查。如果只需要第一个位置或其他内容,也可以添加一个变量并将其在for in循环中递增,以限制循环。
Mihai answered 2019-11-18T00:40:07Z
0 votes
就我而言,这是一个简单的类型
$_SESSION['role' == 'ge']
我缺少正确的右括号
$_SESSION['role'] == 'ge'
Hammad Khan answered 2019-11-18T00:40:42Z
0 votes
如果您使用的是dompdf / dompdf,并且在vendor / dompdf / dompdf / src / Cellmap.php中发生错误,则看来我们在update_row_group方法中使用了错误的帧ID。 初步测试似乎证实了这一点。 虽然这可能是因为严格来说这是分页表格问题,但我的测试床中没有太多文档具有分页表格。
您可以尝试将800行更改为:
$ r_rows = $ this-> _ frames [$ g_key] [“ rows”];
($ g_key代替$ r_key)
[https://github.com/dompdf/dompdf/issues/1295]
Madhavi Khatal answered 2019-11-18T00:41:35Z
-2 votes
它只是一个警告用途:
error_reporting(0);
它显示了我们何时不初始化数组并将值直接分配给索引。
somefunction{
$raja[0]="this";
$raja[1]="that";
}
相反:
somefunction{
$raja=array(0=>'this',1='that');
//or
$raja=array("this","that");
}
它只是通知,不会产生任何输出错误或任何意外的输出。
Teerath Kumar answered 2019-11-18T00:42:24Z