tags:

views:

91

answers:

2

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?

+4  A: 

Are you sure it's a multidimensional array? I think that if you access a string like an array it returns the character at the key's position in the string, and that could be your problem. Check your assignments and whatnot and make sure you're not accidentally assigning a string to $matches[1] instead of an array of strings.

Annath
Thanks. I was calling preg_match() instead of preg_match_all() so my array was not multidimensional after all. In the future, good to know this is indicative of string access. Thanks!
chipotle_warrior
A: 

That is strange! I have never had that happen before. I wonder if the multidimensional really is just a normal array. It could be using your [$i] to return a substring if it is just a single dimension.

Josh Curren