Search code examples
phparraysinterpolationcurly-braces

Interpolation of arrays in php


A simple question for pros. There is a code with which we are getting access to an array value:

foreach($basket as $k=>$v)
 echo  "{$v[0]['title']} <br>";



it prints to browser:
 book 1
 book 2
 etc...

and now, if we delete the braces such as this:

echo  "$v[0]['title'] <br>";

it prints:

array['title']
array['title']
etc...

so interpreter "sees" $v[0] and this is an array yes. but he can't get access to ['title'].

plz tell newbie, why does it happen? i know that braces interpolate a variables in cases when there are some letters around. But there are no letters around here.


Solution

  • That's because echo first considers $v[0] as variable and then prints it's STRING value which is array and then ['title'] gets interpreted as a plain text string.