我无法获取保留在 $_SESSION 变量中的值

发布于 2024-11-13 06:44:19 字数 2208 浏览 3 评论 0原文

我试图从索引数组中获取值,但是当我使用变量作为索引(等于数组中的索引)时,它不会出现。我收到未定义索引错误。为什么会发生这种情况?有谁知道吗?谢谢!

我有以下代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-11-20 06:44:19

mysql 查询的结果是字符串,即使它们在数据库中是整数。这会导致您的 $bookarray 通过数字字符串而不是数字索引关联,您可能会尝试对其使用数字索引。

尝试将该 ID 转换为 int:

$bookarray[(int) $row['bookID']] = array(

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:

$bookarray[(int) $row['bookID']] = array(
坏尐絯 2024-11-20 06:44:19

如果我查看您问题的来源,数字之前似乎有一个换行符(因此,它将是 "\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 of 5, which logically cannot be found), can you confirm this? What does a var_dump($cartedbook); yield?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文