开发者

PHP echo construct - array problem [closed]

开发者 https://www.devze.com 2023-01-23 09:11 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect 开发者_StackOverflow社区answers to be supported by facts, references,or expertise, but this question will
As it currently stands, this question is not a good fit for our Q&A format. We expect 开发者_StackOverflow社区answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.
echo "$gooshgoosh[$i]['num']";

Does anyone here know why it echos

array['num']


PHP will only evaluate interpolated expression to the first array index, or the first attempt to dereference an object. This means that these won't work as expected:

echo "$array[id1][id2]";    // like "{$array[id1]}[id2]"
echo "$object->obj1->obj2"; // like "{$object->obj1}->obj2"

You can force PHP to evaluate the entire expression using curly braces:

echo "{$array[id1][id2]}";
echo "{$object->obj1->obj2}";

In your particular case, PHP was evaluating "$gooshgoosh[$i]", which resolved to an array. Array to string conversion yields the string "array", so that string was substituted in yielding "array['num']".


This happens because of quotes: PHP does not understand, that ['num'] is array index. Try this:

echo $gooshgoosh[$i]['num'];
0

精彩评论

暂无评论...
验证码 换一张
取 消