multiple arrays in a foreach statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lehula
Forum Newbie
Posts: 7
Joined: Sat Jul 14, 2007 4:33 pm

multiple arrays in a foreach statement

Post by lehula »

I want to echo the array $tables[1] and array $tables[2]. I have && signs to show what I'm trying to do.
This obviously doesn't work, but how can I get it to work?


preg_match_all('/(<table id="friendtable(..).{150,180}font-size:.?.?.?.?pt">)<a/', $text, $tables);
foreach($tables[1] as $whole && $tables[2] as $number)
{

echo $whole;
echo $number;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

From where and how do you get the array $table?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could run through them separately and then merge them.

Code: Select all

$tmp = array();
$index = 0;

foreach($table[1] as $value)
{
    $tmp[$index++] = $value;
}

$index = 0;

foreach($table[2] as $value)
{
    $tmp[$index++] = array($tmp[$index], $value);
}
And then echo them together via the $tmp array. I'm not sure if the increment in the second loop should happen on the left or right side of the assignment... You should take a look into it.
Post Reply