I see this problem on and off again in my PHP coding, and I've never understood what's happening.
$val = $matches[1][$i]
In this example, $i = 0 (set in a for loop) and the value held in that dimension of the array is a string. Instead of that string being assigned to $val, $val gets assigned the 0th (first) character in the string. If $i = 1, $val gets assigned the 1st (second) character in the string, etc.
Instead of returning the string, why is PHP treating this like a substring operation? How can I get my string value instead of a single character in that string?