php - Access a variable using a counter as part of the variable name -


i tried somthing that:

$cat1 = array('hello', 'everyone'); $cat = array('bye', 'everyone');  for($index = 0; $index < 2; $index++) { echo $cat$index[1]; } 

it doesn't work of course. need change here?

you should use nested arrays, can done.

$cat1 = array('hello', 'everyone'); $cat2 = array('bye', 'everyone');  for($i = 1; $i <= 2; $i++) {     echo ${'cat' . $i}[1]; } 

reference: http://php.net/language.variables.variable

this better though:

$cats = array(     array('hello', 'everyone'),     array('bye', 'everyone') ); foreach ($cats $cat) {     echo $cat[1]; } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -