I tried to do somthing like that:
$cat1 = array(\'hello\', \'everyone\');
$cat = array(\'bye\', \'everyone\');
for($index = 0; $index < 2; $index++) {
echo $
Is this what you intended?
$cat0 = array('hello', 'everyone');
$cat1 = array('bye', 'everyone');
for($index = 0; $index < 2; $index++) {
$varname = 'cat'.$index;
echo $varname[0].' '.$varname[1];
}
You can't reference $index like that, it isn't an array.
echo $cat[$index];
is what you want to do.
To echo elements inside arrays, you need to be using
echo $cat[$index]
with your example.
I'm not sure what the $index[1]
is supposed to be doing? Maybe I have misunderstood your question.
If you insist on doing it this way...
echo ${'cat' . $index}[1];
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];
}