Reputation: 13
I have the following arrays and loop conditions. I cannot preview this output.
Wanted Output: (this doesn't answer the code I provided below)
emp_name1 = captamerica
emp_name2 like ironman
my php code:
$filter = array('emp_name1','emp_name2');
$condition = array('=','like');
$values = array('captamerica','ironman');
foreach($filter as $row){
foreach($condition as $row2){
foreach($values as $row3){
echo $row." ".$row2." ".$row3."<br>";
}
}
}
Upvotes: 0
Views: 1310
Reputation: 2285
You can use multiple foraech. but not to display this kink of output.
you have 3 nested loops, each will loop 2 times, which means than echo statement will be executed 2*2*2=8 times.
to print the tow lines you want you will simply need one for loop
statement.
for($i=0;$i<2;$i++)
echo $filter[$i]." ".$condition[$i]." ".$values[$i]."<br>";
Upvotes: 0
Reputation: 121
You need to have same element count for each array, which is ok for your data. Here is what you need :
foreach($filter as $k=>$row){
echo $row." ".$condition[$k]." ".$values[$k]."<br>";
}
Upvotes: 1
Reputation: 1681
As long as you doing it on different arrays you can do multiple foreach. The reason is that php has internal array pointers which need to be manipulated during a foreach. If you do multiple foreach on the same array the internal pointers will be disturbed.
I don't get the output you have cited. What I get is :
emp_name1 = captamerica
emp_name1 = ironman
emp_name1 like captamerica
emp_name1 like ironman
emp_name2 = captamerica
emp_name2 = ironman
emp_name2 like captamerica
emp_name2 like ironman
Upvotes: 0