I have two arrays:
$my_array1= array("A", "B");
$my_array2= array("1", "2");
Is it possible to use variables ($x) in the index of the array like this:
for ($x = 1; $x <= 2; $x++) {
Echo $my_array.$x[0];
}
How to achieve this?
I have two arrays:
$my_array1= array("A", "B");
$my_array2= array("1", "2");
Is it possible to use variables ($x) in the index of the array like this:
for ($x = 1; $x <= 2; $x++) {
Echo $my_array.$x[0];
}
How to achieve this?
After referred in comment , i think it's work : here
$my_array1= array("A", "B");
$my_array2= array("1", "2");
and re-use your loop like:
for ($x = 1; $x <= 2; $x++) {
// to init the new name of array
$init = 'my_array'.$x;
// to use variable in the name of variable
Echo $$init[0];
}
I hope it's help you
I think what you're looking for is to be able to use key / value pairs.
You can do something like:
$array = array(
1 => "a",
2 => "b",
3 => "c",
4 => "d",
);
foreach ($array as $key => $value) {
echo "{$key} => {$value} ";
}
Or loop througout the numeric key values as you like.