<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >How could I count the elements of each dimension of an array? >I have this script, but it failed, thanks for any help >$ojpp[0] = "1"; >$ojpp[1] = "2"; >$ojpp[2] = "3"; >$ojpp[3] = "4"; >$ojpp[0][1] = "1"; >$ojpp[0][2] = "2"; >$ojpp[0][3] = "3"; >$ojpp[0][0][1] = "1"; >$ojpp[0][0][2] = "2"; >$test1 = count($ojpp); // first dimension >$test2 = count($ojpp[0]); // second dimension >$test3 = count($ojpp[0][0]); // third dimension >echo $test1; >echo $test2; >echo $test3; >echo "<pre>"; >print_r($ojpp); >echo "</pre>";
The problem with the code above isn't with how you count the dimensions of the array, but how you assign values to the second and third dimensions. Ordinarily, the code $ojpp[0][1] = "1" will create an array at $ojpp[0] and set it's second element to be "1". On the first line of your code, however you have *already* assigned the string value "1" to $ojpp[0]. So when PHP sees the code $ojpp[0][1] = "1" it thinks you're trying to use the 'curly braces' syntax for accessing single characters of a string. This syntax is not widely known, but essentially means you can access characters in a string like elements of an array, by using curly braces. For example,<? echo $string{2}; ?> will print the third character in $string. For reasons unknown to me, PHP will also let you use square brackets on a string to perform the same action ... eg <? echo $string[2]; ?> although this alternate syntax is being phased out. ( For more info see: http://us2.php.net/manual/en/language.types.string.php ). So essentially PHP intereperets $ojpp[0][1] = "1" as being an attempt to assign "1" as the second character to the string already assigned to $ojpp[0]. Which begs the question: why assign "1" to $ojpp in the first line of your code when you seem to want to reaplce it with an array on lines 5 - 8? This kind of coding bug is a by-product of a type-juggling, permissive languge that doesn't require you to pre-declare variables. To fix your code, change the first line of your code to read: $ojpp[0] = array(); Hope that helps, Al -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php