Here's the code:
<?
$C1=array("a"=>1,"b"=>2);
$C2=array("z"=>1,"s"=>2);
$C3=array("x"=>1,"h"=>2);
$C4=array("a"=>1,"c"=>2);
$keywords=array("x","z","h");
for($i=2;$i<4;$i++)
{
$i="C".$i;
$array=$$i;
foreach($keywords as $val)
{
if(isset($array[$val]))
{
echo $i." -> $val<br>";
}
}
}
?>
It should show:
C2 -> z
C3 -> x
C3 -> h
If i write $i=2
instead of the for()
loop it writes C2 -> z
, as it should.
But I have to use the for()
loop that generates an infinite loading.
Why? Where's the problem?
Inside the loop, you're reassigning $i
variable with a string. After that it doesn't pass the loop boundary check. Instead, use another variable:
<?
$C1=array("a"=>1,"b"=>2);
$C2=array("z"=>1,"s"=>2);
$C3=array("x"=>1,"h"=>2);
$C4=array("a"=>1,"c"=>2);
$keywords=array("x","z","h");
for($i=2;$i<=4;$i++)
{
$a="C".$i;
$array=$$a;
foreach($keywords as $val)
{
if(isset($array[$val]))
{
echo $a." -> $val<br>";
}
}
}
?>
In addition, it's worth noting that your loop would only run twice (for $i equal 2 and 3), as you're comparing $i<4
. So in my code I changed this to $i<=4
to fix this.
At first $i is a number and then $i++ increments as expected but then you change $i to "C".$i
so that $i="C2" now i guess that ++ operator applied to C2 value never makes it >4