Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional array undefined index problem

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
                )

        )
)
like image 326
Shoaib Iqbal Avatar asked Aug 23 '11 14:08

Shoaib Iqbal


2 Answers

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

like image 163
Felix Kling Avatar answered Nov 07 '22 03:11

Felix Kling


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.

like image 37
Madara's Ghost Avatar answered Nov 07 '22 03:11

Madara's Ghost