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
)
)
)
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 toname="chapter[0][photo_caption]"
solved the problem
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With