我无法获取保留在 $_SESSION 变量中的值
我试图从索引数组中获取值,但是当我使用变量作为索引(等于数组中的索引)时,它不会出现。我收到未定义索引
错误。为什么会发生这种情况?有谁知道吗?谢谢!
我有以下代码:
$bookarray = array();
$books = mysqli_query($db, "SELECT * FROM books");
while($row = mysqli_fetch_assoc($books)){
$bookarray[$row['bookID']] = array(
'title' => $row['title'],
'author' => $row['author'],
'price' => $row['price']
);
}
echo"<hr>";
print_r($bookarray);
echo"<hr>";
echo $bookarray[5]['title']."<br />";
foreach($_SESSION['cart'] as $cartedbook){
echo $cartedbook;
echo $bookarray[$cartedbook]['title'];
echo "<br />";
}
这是输出:
图书
Array
(
[1] => Array
(
[title] => Java 2 for Pro Deelopers
[author] => Michael Morgan
[price] => 34.99
)
[2] => Array
(
[title] => Installing XAMPP
[author] => Thomas Down
[price] => 24.99
)
[3] => Array
(
[title] => Alice Through the Looking Glass
[author] => Louis Carroll
[price] => 72.35
)
[4] => Array
(
[title] => Quantum Mechanics in 124 Hours
[author] => Neils Bohr
[price] => 24.99
)
[5] => Array
(
[title] => PHP For Fun And Profit
[author] => Thomas Shenk
[price] => 49.99
)
[28] => Array
(
[title] => Test
[author] => Eric Gross
[price] => 100.00
)
)
PHP For Fun And Profit
5
Notice: Undefined index: 5 in C:\xampp\htdocs\FinalProject\cart.php on line 52
3
注意:未定义索引: 3 在 C:\xampp\htdocs\FinalProject\cart.php 中,第 52
标题 价格
I am trying to get values out of an indexed array, but when I use a variable as the index (which is equal to an index in the array), it does not appear. I get an Undefined Index
Error. Why is this happening? Does anyone know? Thanks!
I have the following code:
$bookarray = array();
$books = mysqli_query($db, "SELECT * FROM books");
while($row = mysqli_fetch_assoc($books)){
$bookarray[$row['bookID']] = array(
'title' => $row['title'],
'author' => $row['author'],
'price' => $row['price']
);
}
echo"<hr>";
print_r($bookarray);
echo"<hr>";
echo $bookarray[5]['title']."<br />";
foreach($_SESSION['cart'] as $cartedbook){
echo $cartedbook;
echo $bookarray[$cartedbook]['title'];
echo "<br />";
}
This is the output:
Books
Array
(
[1] => Array
(
[title] => Java 2 for Pro Deelopers
[author] => Michael Morgan
[price] => 34.99
)
[2] => Array
(
[title] => Installing XAMPP
[author] => Thomas Down
[price] => 24.99
)
[3] => Array
(
[title] => Alice Through the Looking Glass
[author] => Louis Carroll
[price] => 72.35
)
[4] => Array
(
[title] => Quantum Mechanics in 124 Hours
[author] => Neils Bohr
[price] => 24.99
)
[5] => Array
(
[title] => PHP For Fun And Profit
[author] => Thomas Shenk
[price] => 49.99
)
[28] => Array
(
[title] => Test
[author] => Eric Gross
[price] => 100.00
)
)
PHP For Fun And Profit
5
Notice: Undefined index:
5 in C:\xampp\htdocs\FinalProject\cart.php on line 52
3
Notice: Undefined index:
3 in C:\xampp\htdocs\FinalProject\cart.php on line 52
Title Author Price
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mysql 查询的结果是字符串,即使它们在数据库中是整数。这会导致您的
$bookarray
通过数字字符串而不是数字索引关联,您可能会尝试对其使用数字索引。尝试将该 ID 转换为 int:
Results from mysql queries are strings, even if they are integers in the database. This causes your
$bookarray
to be associated by numeric strings instead of numeric indexes, which your probably trying to use against it.Try casting that ID as an int:
如果我查看您问题的来源,数字之前似乎有一个换行符(因此,它将是
"\n5"
而不是5
,从逻辑上讲找不到),你能确认一下吗?var_dump($cartedbook);
会产生什么结果?If I look at the source of your question, it seems there is a newline character before the number (so, it would be
"\n5"
instead of5
, which logically cannot be found), can you confirm this? What does avar_dump($cartedbook);
yield?