Multidimensional array keys returning single characters rather than values

I see this problem over and over in my PHP coding and I never really understood what was going on.

$val = $matches[1][$i]

      

In this example, $ i = 0 (set in the for loop), and the value contained in this dimension of the array is a string. Instead of this string being assigned to $ val, $ val gets the 0th (first) character in the string. If $ i = 1, $ val gets the 1st (second) character in the string, etc.

Instead of returning a string, why does PHP treat this as a substring operation? How can I get my string value instead of one character in this string?

0


a source to share


3 answers


Are you sure this is a multidimensional array? I think that if you access a string like an array, it returns the character at the key position of the string and that might be your problem. Check your assignments and more and make sure you don't accidentally assign the string $ matches [1] instead of an array of strings.



+4


a source


This is strange! This has never happened before. I wonder if multidimensional is really just a normal array. It can use your [$ i] to return a substring if it's just one dimension.



0


a source


Man, I know him very late, but please check

$matches[] = "value";

      

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

-1


a source







All Articles