PHP echo construct - array problem [closed]
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'];
精彩评论