Possible Duplicate:
Iterate through two associative arrays at the same time in PHP
I have two arrays $name[] and $type[]. I want to print those arraya through foreach loop like this,
<?php
foreach($name as $v && $type as $t)
{
echo $v;
echo $t;
}
?>
I know this is wrong way then tell me correct way to do this.
You cannot do this like that. You need to do one of the following:
forloop and “share” the index variable, oreachand friends, orarray_mapExample with
for:Possible issues: The arrays need to be numerically indexed, and if they are not the same length it matters a lot which array you use
counton. You should either target the shorter array or take appropriate measures to not index into the longer array out of bounds.Example with
each:Possible issues: If the arrays are not the same length then this will stop processing elements after the “shorter” array runs out. You can change that by swapping
||for&&, but then you have to account for one of$nand$tnot always having a meaningful value.Example with
array_map:Possible issues: If the two arrays are not the same length then the shorter one will be extended with nulls; you have no control over this. Also, while convenient this approach does use additional time and memory.