NYCPHP Meetup

NYPHP.org

[nycphp-talk] Bogus or not?

Tim McEwen tim at tmcode.com
Wed Sep 27 11:37:54 EDT 2006


The reply to the bug is correct,  PHP is doing what its supposed  
to.    When you are executing the line:

$test_array['name'] = 'value';

You are declaring $test_array['name']  as a string.   Then when you  
access that string using brackets you are telling PHP that you want a  
character from that string.  For example:

$test_array['name'][1]

should and would give you 'a'.    The confusing part is that you are  
using a text key.  Since $test_array['name']  is a string,  PHP is  
expecting the value in brackets to be an integer.  Consequently php  
does a type conversion on your 'count' key turning it into 0.  0 in  
this case corresponds to the 'v' in value.  Your statement is  
actually the equivalent of

$test_array['name'][0] = '2';


To "correct" your code, you have to force php to change the type of  
$test_array['name'].  This would be done by reassigning the value to  
array():


$test_array['name'] = array();
$test_array['name']['count'] = '2';



That all being said,  I would agree that PHP could be slightly more  
intelligent and see the fact that you passed a text key.  Seeing that  
you passed a text key,  that would imply you want to type convert  
that element from string to an array.    My guess is that you are  
going to have a tough time convincing anyone to make the modification  
tho.

-Tim




On Sep 27, 2006, at 11:22 AM, Jonathan Hendler wrote:

> I filed this "bug"/feature request.
>
> Arrays don't properly overwrite strings in multi-dimensional arrays.
> PHP folks feel that everything works as it should.
>
> Any thoughts?
>
> http://bugs.php.net/bug.php?id=38974
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php




More information about the talk mailing list