-4

Possible Duplicate:
Printing a multi dimensional array using Foreach loop only
Merge multiple associative arrays to a single array of associative arrays

I have a script that loops scraping links from different pages and places them into an array with different keys. Now I want the script to echo all the final links into a list. As of now the only way I know to do this is like this:

foreach($matches[0][1] as $match) {
   echo "$match<br />";
}

foreach($matches[1][1] as $match) {
  echo "$match<br />";
}

foreach($matches[2][1] as $match) {
  echo "$match<br />";
}

How can I have it do something where I don't have to do a foreach loop for each array key. Something like

foreach($matches[ALL][1] as $match) {
}
Community
  • 1
  • 1
Dan
  • 31
  • 1
  • 6

2 Answers2

1

This wil work

foreach($matches as $var)
{
  foreach($var[1] as $match)
  {
    echo "$match<br />";
  }
}
Naveed
  • 1,191
  • 10
  • 22
1

Simple:

foreach($matches as $match) {
  foreach($match[1] as $m) {
    echo $m . "<br />";
  }
}

That's it: Iterate over the first level array ($matches) and get the values. Those values are also arrays, so you can access the [1] item.

If you need the index for the first array, modify the loop like this:

foreach($matches as $k => $match) {
  foreach($match[1] as $m) {
    echo $k . ": " . $m . "<br />";
  }
}

Hope that helped!

felixgaal
  • 2,403
  • 15
  • 24