0

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?

1
  • How are you defining the array matches? Please post ample of the loop with offending code snippet Commented May 14, 2009 at 3:05

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
0

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.

Comments

-1

Dude, I know its very late but please check

$matches[] = "value";

If you are missing [], then it would be treated as a string and so should be your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.