|
Posted by David Gillen on 10/23/06 16:09
An noise sounding like Mateusz Markowski said:
> stefano.troiani@gmail.com napisal(a):
>> Hi,
>>
>> i have two arrays and I would like to loop both of them one inside the
>> other.
>> The first one comes from a readdir and present an array with the
>> document's names, the second one is an array with the names i want to
>> appear for that documents as a link.
>> My need is to sort both of them so that i got something like this:
>> <a href="first element of array 1" > first element of array 2</a>
>> <a href="second element of array 1" > second element of array 2</a>
>> .........................................
>>
>> but i don't know how to do it.
>
> $array1 = array('first', 'second');
> $array2 = array('one', 'two');
> for ($i=0; $i<count($array1); $i++) {
> echo "<a href=\"".$array1[$i]."\" >".$array2[$i]."</a>";
> }
>
foreach($array1 as $key=>$val)
{
echo "<a href=\"".$array1[$key]."\" >".$array2[$key]."</a>";
}
No over head of $i, $i < comparison, and $i increment.
foreach is a far too underused language construct. The other solutions posted
are perfectly workable, I just have a strong preference for foreach when
working with arrays.
Dave.
--
[Back to original message]
|