473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

looping two arrays

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.

Thanks for any help,

stefano

Oct 23 '06 #1
6 1352
st*************@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>";
}

Oct 23 '06 #2
st*************@gmail.com wrote:
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.

Thanks for any help,

stefano
Hi Stefano,

That is very straightforward.
I assume the arrays are both the same length.
(if not your setup makes little sense.)

<?
// both arrays are filled by your code
$arrHyperlink = array(.....);
$arrHyperlinkText = array(.....);

for ($i=0;$i<count($arrHyperlink);$i++){
?>
<a href="<?= $arrHyperlink[$i] ?>"><?= $arrHyperlinkText[$i] ?></a>
<?
}
?>

Simple he? Or did I miss someting? (Happens the whole day to me. :P)

Regards,
Erwin Moller
Oct 23 '06 #3
st*************@gmail.com wrote:
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.

Thanks for any help,

stefano

foreach( paths_array AS $key =$path ){

// if document in path exists
if( file_exists($path) ){

// if there is a name for this document, set defined name else set name as "no name"
( $names_array[$key] ) ? $name = $names_array[$key] : $name = "no name";

// print link with document path and document name
print "<a href=\"".$path."\">$name</a>";

}

}
Oct 23 '06 #4
Rik
David Gillen wrote:
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.
Indeed, and it works with named keys, also a handy feature..
--
Rik Wasmus
Oct 23 '06 #5
Rik
st*************@gmail.com wrote:
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>
.........................................
How exactly are you linking the document names and descriptions?
If link them purelay on the numerical indexes in both arrays, with one to
one relations, try:

$combined = array_map(null,$files,$desciptions);
foreach($combined as $link){
vprintf('<a href="%s">%s</a>',$link);
}
--
Grtz,

Rik Wasmus
Oct 23 '06 #6

Rik wrote:
st*************@gmail.com wrote:
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>
.........................................

How exactly are you linking the document names and descriptions?
If link them purelay on the numerical indexes in both arrays, with one to
one relations, try:

$combined = array_map(null,$files,$desciptions);
foreach($combined as $link){
vprintf('<a href="%s">%s</a>',$link);
}
--
Grtz,

Rik Wasmus

Thanks!!! this is what I was looking for :)

stefano

Oct 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: paul | last post by:
Hi all, I have a 2D array, which I am trying to access, but I am having problems doing this: If, for instance, I use a nested for loop to go through the values, using something like echo...
4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
2
by: Amjad | last post by:
I want to copy 1-D arrays to columns of a 2-D array. Is there a way of doing that without using a loop that will copy element by element?
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
4
by: nico3334 | last post by:
I'm having problems with my code concerning a mulitdimensional array. Basically, I'm trying to create an array (arrTotal) which contains all of the data from 2 arrays (arrOne & arrTwo). Basically,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.