Array and foreach
arrays, php
Solution
foreach takes each element of the array and assigns it to the variable. To get the results I assume you are expecting you just need to do:
foreach ($posts as $post) {
echo $post;
}
The specifics as to why your code didn't work: `$post` would be the contents of the array element - in this case a string. Because PHP isn't strongly typed / supports type juggling, you can in fact work with a string as if it were an array, and get to each character in the sequence:
foreach ($posts as $post) {
echo $post[0]; //'t'
echo $post[1]; //'h'
}
Obviously `$post['message']` therefore is not a valid element, and there is no explicit conversion from `(string)'message'` to `int`, so this evals to `$post[0]`.
Problem
``` $posts = array( "message" => 'this is a test message' ); foreach ($posts as $post) { echo $post['message']; } ``` Why does the above code only output the first letter in message? "t". Thanks!