Forum Moderators: coopster

Message Too Old, No Replies

Parallel output of arrays

         

orion_rus

9:31 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



I need to make output 3 arrays in one table.
It should be seems like so:
<tr>
<td>array1['new1']</td><td>array2['old1']</td><td>array3['some1']</td>
<td>array1['new2']</td><td>array2['old2']</td><td>array3['some2']</td>
and so on. To speak other words it should be 3 parallel collumns and different array values in it
Can anybody help me? how i can show it so?

paladin

9:46 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



You can try and use a loop. So instead of
<td>array1['new1']</td><td>array2['old1']</td><td>array3['some1']</td>

you will be outputting somethng like:
print "<td>array1[$i]</td><td>array2[$i]</td><td>array3[$i]</td>";
$i++;

orion_rus

9:50 am on Dec 31, 2004 (gmt 0)

10+ Year Member



but if index is not numeric? it's like 'sport','travel' and so on!

Salsa

10:37 am on Dec 31, 2004 (gmt 0)

10+ Year Member



If all the arrays have the same sets of indexes, you could use foreach, like:

foreach($array1 AS $key => $value) { 
<td>$value</td><td>array2[$key]</td><td>array3[$key]</td>
}

If the arrays don't have the same sets of indexes (as they don't in your example), but each index has a numerical component that advances by one per row (as in your example), you could first run some loops to get rid of the text parts of the indexes (or don't include the text in the first place).

jollymcfats

4:38 pm on Dec 31, 2004 (gmt 0)

10+ Year Member



If the arrays are the same length and in the order you need, here's one way to loop in parallel.

for ($i=0; $i < count($array1); $i++) {
$val1 = array_shift($array1);
$val2 = array_shift($array2);
$val3 = array_shift($array3);
print "<td>$val1</td><td>$val2</td><td>$val3</td>";
}

The above will destroy the arrays. You could make copies if that was an issue.