I tried to do somthing like 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. What do I need to change here?
I tried to do somthing like 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. What do I need to change here?
You should use nested arrays, but this can be 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 would be much better though:
$cats = array(
array('hello', 'everyone'),
array('bye', 'everyone')
);
foreach ($cats as $cat) {
echo $cat[1];
}