Multidimensional array undefined index problem

I get a multidimensional array from an HTML form. When I want to get a single value, for example

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

he says undefined index title .

When I print an array, it shows how

 Array ( [chapters] => Array ( [0] => Array ( ['title'] => this is title ['text'] => this is text ['photo'] => this is photo source ['photo_caption'] => photo caption ) ) ) 
+4
source share
3 answers

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

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

This should work:

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

but it’s better to fix the keys.

From your comment:

the problem was that I used single quotes ( name="chapter[0]['photo_caption']" ) in the html form, adjusted to name="chapter[0][photo_caption]" , solved the problem

+4
source

According to your result, you should use $chapters["chapters"][0]["title"] .

Please note that there are 3 nested arrays on your output, so you need to go through 3 levels to get your value.

+4
source

Yes, I ran into the same problem. Then I realized what I was doing wrong with the keys. In fact, I used a quote by naming the form elements as an array

Example -

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

I adjusted and removed the quotes below

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

It was a small mistake. Deleted quotes, and it worked then.

0
source

All Articles