How can the PREG_SET_ORDER flag in preg_match_all() affect the output of the function in PHP?

The PREG_SET_ORDER flag in preg_match_all() changes the order of the output array. By default, the function returns matches grouped by pattern first, then by occurrence. When using PREG_SET_ORDER, the output array will be grouped by occurrence first, then by pattern. To fix the issue and maintain the default behavior, simply omit the PREG_SET_ORDER flag when calling preg_match_all().

// Incorrect usage with PREG_SET_ORDER flag
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);

// Correct usage without PREG_SET_ORDER flag
preg_match_all($pattern, $subject, $matches);