6

I am getting a multidimensional array from an HTML form. When I want to get a single value, e.g.

$chapters = $_POST["chapters"];

echo $chapters[0]["title"];

it says undefined index title.

When I print the array, it shows as

Array
(
    [chapters] => Array
        (
            [0] => Array
                (
                    ['title'] => this is title
                    ['text'] => this is text
                    ['photo'] => this is photo source
                    ['photo_caption'] => photo caption
                )

        )
)
2
  • 1
    (a) Which array are you printing? $_POST or $chapters? (b) If this is the exact output, why are the string keys one time in quotes (e.g. ['title']) and the other time without quotes ([chapters]) ? print_r never prints string keys with quotes, so you must have manipulated the output. Commented Aug 23, 2011 at 14:58
  • possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Mar 29, 2013 at 11:56

3 Answers 3

6

Based on your comments, the problem seemed to be following:

print_r never prints quotes for string keys. If you have not manipulated the output somehow, then it can only mean that the single quotes are actually part of the key.

This should work:

echo $chapters[0]["'title'"];

but better you fix the keys.

From your comment:

the problem was that i was using single quotes (name="chapter[0]['photo_caption']") in html form, corrected to name="chapter[0][photo_caption]" solved the problem

Sign up to request clarification or add additional context in comments.

Comments

4

According to your output, you should be using $chapters["chapters"][0]["title"].

Note that you have 3 nested arrays in your output, therefore you'll need to go 3 levels deep to get your value.

3 Comments

$chapters["chapters"][0]["title"] :)
thanks Felix Kling, the problem was that i was using single quotes (name="chapter[0]['photo_caption']") in html form, corrected to name="chapter[0][photo_caption]" solved the problem, many many thanks
@Shoaib: Ah, so the they keys literally contained single quotes? That was tricky...
0

Yeah, I faced the same problem. Then I realized, I was doing wrong with the keys. Actually, I was using the quote while naming the form elements as an array

Example-

echo "<input type='hidden' name= userprogramscholarship[$user->id]['checkstatus'] value= $val />";

I corrected and removed quotes as below

echo "<input type='hidden' name= userprogramscholarship[$user->id][checkstatus] value= $val />";

It was minor mistake. Removed quotes and it worked then.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.