Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:48:39+00:00 2026-05-23T02:48:39+00:00

I am trying to get values out of an indexed array, but when I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:48:40+00:00Added an answer on May 23, 2026 at 2:48 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the values from a pointer to a float array, but
I'm trying to get random values out of an array and then break them
Trying to get a value out of 2d array inside of a hash and
I'm working on a script and trying to get some values from an array
Im trying to work out how to get the value from one setting in
I am trying to work out how to get the value of table cell
I'm trying to figure out how to get the value from a method in
I'm trying to figure out how to get the changed value to submit to
I am trying to figure out how I can get a property value from
I'm trying get values from a GridView using the following code: foreach (GridViewRow row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.