What potential issue arises when using preg_match_all() in combination with foreach() in PHP?

When using preg_match_all() in combination with foreach() in PHP, the potential issue that arises is that the foreach loop iterates over all matches returned by preg_match_all(), including the full match and captured groups. To avoid this issue and only iterate over captured groups, you can use the PREG_SET_ORDER flag in preg_match_all() to organize the matches into an array of match groups, each containing all captured groups for a single match.

// Example code snippet to iterate over captured groups using preg_match_all() and foreach loop

// Input string
$input = "Hello, World!";

// Regular expression to match words
$pattern = '/(\w+)/';

// Perform a global regular expression match and store captured groups
preg_match_all($pattern, $input, $matches, PREG_SET_ORDER);

// Iterate over captured groups
foreach ($matches as $match) {
    // Access captured groups using index
    echo $match[1] . "\n";
}