php 注意:未定义的偏移量:0 in
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6549561/
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
Notice: Undefined offset: 0 in
提问by louismoore18
I am getting this PHP error, what does it mean?
我收到此 PHP 错误,这是什么意思?
Notice: Undefined offset: 0 in
C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
From this code:
从这个代码:
<?php
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!";
}
?>
回答by YonoRan
You are asking for the value at key 0
of $votes
. It is an array that does not contain that key.
你问在关键的价值0
的$votes
。它是一个不包含该键的数组。
The array $votes
is not set, so when PHP is trying to access the key 0
of the array, it encounters an undefined offset for [0] and [1] and throws the error.
数组$votes
未设置,因此当 PHP 尝试访问0
数组的键时,它遇到 [0] 和 [1] 的未定义偏移量并抛出错误。
If you have an array:
如果你有一个数组:
$votes = array('1','2','3');
We can now access:
我们现在可以访问:
$votes[0];
$votes[1];
$votes[2];
If we try and access:
如果我们尝试访问:
$votes[3];
We will get the error "Notice: Undefined offset: 3"
我们将收到错误“注意:未定义偏移量:3”
回答by Dmitrii Malyshev
This answer helped me https://stackoverflow.com/a/18880670/1821607The reason of crush — index 0 wasn't set. Simple $array = $array + array(null)
did the trick. Or you should check whether array element on index 0 is set via isset($array[0])
. The second variant is the best approach for me.
这个答案帮助了我https://stackoverflow.com/a/18880670/1821607粉碎的原因 - 未设置索引 0。简单$array = $array + array(null)
的伎俩。或者您应该检查索引 0 上的数组元素是否通过isset($array[0])
. 第二种变体对我来说是最好的方法。
回答by Agg-rey Muhebwa
first, check that the array actually exists, you could try something like
首先,检查数组是否确实存在,您可以尝试类似
if (isset($votes)) {
// Do bad things to the votes array
}
回答by Giulio Prisco
Use print_r($votes);
to inspect the array $votes
, you will see that key 0
does not exist there. It will return NULL and throw that error.
使用print_r($votes);
检查数组$votes
,你会看到,关键0
不存在那里。它将返回 NULL 并抛出该错误。
回答by Tim Cooper
getAllVotes()
isn't returning an array with the indexes 0
or 1
. Make sure it's returning the data you want by calling var_dump()
on the result.
getAllVotes()
不返回带有索引0
或1
. 通过调用var_dump()
结果确保它返回您想要的数据。
回答by Espanta
As explained it happens because there is no data in the $cur_votes[0]
and hence it throws an error.
正如解释的那样,这是因为 中没有数据$cur_votes[0]
,因此会引发错误。
To ensure your code works fine, before performing "$votes_up = $cur_votes[0]+1;"
echo the $cur_votes[0]
value to see if there is any value stored or not.
为确保您的代码正常工作,在执行"$votes_up = $cur_votes[0]+1;"
echo$cur_votes[0]
值之前查看是否存储了任何值。
Surely, there is no value stored.
当然,没有存储任何值。
回答by Anthony Rutledge
function getEffectiveVotes($id)
According to the function header, there is only one parameter variable ($id
).
Thus, on line 27, the votes[]
array is undefined and out of scope. You need to add another
parameter value to the function header so that function getEffectiveVotes()
knows to expect two parameters. I'm rusty, but something like this would work.
根据函数头,只有一个参数变量($id
)。因此,在第 27 行,votes[]
数组未定义且超出范围。您需要在函数头中添加另一个参数值,以便函数getEffectiveVotes()
知道需要两个参数。我生锈了,但这样的事情会奏效。
function getEffectiveVotes($id, $votes)
I'm not saying this is how it should be done, but you might want to research how PHP passes its arrays and decide if you need to explicitly state to pass it by reference
我并不是说这是应该如何完成的,但您可能想研究 PHP 如何传递其数组并决定是否需要明确声明以通过引用传递它
function getEffectiveVotes($id &$votes) <---I forget, no time to look it up right now.
Lastly, call function getEffectiveVotes()
with both arguments wherever it is supposed to be called.
最后,getEffectiveVotes()
在应该调用的地方使用两个参数调用函数。
Cheers.
干杯。
回答by vijayrana
As you might have already about knew the error. This is due to trying to access the empty array or trying to access the value of empty key of array. In my project, I am dealing with this error with counting the array and displaying result.
正如您可能已经知道的错误。这是由于尝试访问空数组或尝试访问数组的空键的值。在我的项目中,我通过计算数组和显示结果来处理这个错误。
You can do it like this:
你可以这样做:
if(count($votes) == '0'){
echo 'Sorry, no votes are available at the moment.';
}
else{
//do the stuff with votes
}
count($votes)
counts the $votes
array. If it is equal to zero (0)
, you can display your custom message or redirect to certain page else you can do stuff with $votes
. In this way you can remove the Notice: Undefined offset: 0
in notice in PHP.
count($votes)
对$votes
数组进行计数。如果它等于零(0)
,您可以显示您的自定义消息或重定向到某些页面,否则您可以使用它$votes
。这样就可以去掉Notice: Undefined offset: 0
PHP中的in 通知了。
回答by Mihai
I encountered this as well and the solution is simple, dont hardcode the array index position in your code.
Instead of
$data[0]['somekey']
do foreach($data as $data_item)
{
echo $data_item['somekey'];
}
If there is an array or more you can perform your desired action inside the loop, but if it's undefined it will not bring an error. you can also add other checks on top of that.
You could also add a variable and increment it in a for in loop to limit your looping if you want only the first positions or something.
我也遇到了这个问题,解决方案很简单,不要在代码中对数组索引位置进行硬编码。
而不是
$data[0]['somekey']
doforeach($data as $data_item)
{
echo $data_item['somekey'];
}
如果有一个或多个数组,您可以在循环内执行您想要的操作,但如果它未定义,则不会带来错误。您还可以在此基础上添加其他检查。如果您只想要第一个位置或其他东西,您还可以添加一个变量并在 for in 循环中增加它以限制您的循环。
回答by Hammad Khan
In my case it was a simple type
在我的情况下,它是一个简单的类型
$_SESSION['role' == 'ge']
I was missing the correct closing bracket
我错过了正确的右括号
$_SESSION['role'] == 'ge'