[nycphp-talk] Array handling - Why does this happen?
Dan Cech
dcech at phpwerx.net
Thu Feb 12 12:58:32 EST 2004
It's not immediately clear what's going on in the code you presented,
but I have extracted the relevant code, which is:
$answer = array (
'Yes' => array (
'Yes' => 'Yes',
'count' => 0
),
'No' => array (
'No' => 'No',
'count' => 0
)
);
$val='Yes';
$val1='No';
$answer[$val]['count']=8888;
echo $answer[$val]['count']."<br>";
$answer[$val1]['count']=9999;
echo $answer[$val1]['count']."<br>";
$answer = array (
'Yes' => 'Yes',
'No' => 'No'
);
$val='Yes';
$val1='No';
$answer[$val]['count']=8888;
echo $answer[$val]['count']."<br>";
$answer[$val1]['count']=9999;
echo $answer[$val1]['count']."<br>";
As you can see, in the first case $answer[$val] is an array, whereas in
the second case $answer[$val] is a string.
What's happening I think is that you are unwittingly invoking the
(deprecated) syntax for accessing a string by character:
$mystring = 'hello';
echo $mystring{0};
echo $mystring[0];
both should result in 'h';
This syntax can also be used to assign into a string...which is what is
happening here.
The 'count' is being to converted to the integer 0, and the first
character in the string is being overwritten with the first character
from the value you are assigning.
So in the second example:
$answer[$val] is 'Yes'
Then we do:
$answer[$val]['count'] = 8888;
which is equivalent to:
$answer[$val]{0} = 8888;
so $answer[$val] is now '8es'
then we do:
echo $answer[$val]['count'];
which is equivalent to:
echo $answer[$val]{0};
which results in '8'
An interesting little problem for sure.
Dan
DeWitt, Michael wrote:
> I have run into a situation with arrays that I can't figure out why value
> assigments are truncated down to one byte.
>
> Consider the array
>
> $question['1']=array('type'=>'radio','showresults'=>1,'required'=>0,'questio
> n'=>'Does your community post signs on neglected properties with the
> owner’s name and address clearly visible to all passersby?',
> 'answer'=>array('Yes'=>array('Yes'=>'Yes','count'=>0),'No'=>array('No'=>'No'
> ,'count'=>0)));
>
> $q_id=1;
> $val='Yes';
> $val1='No';
>
> $question[$q_id]['answer'][$val]['count']=8888;
>
> echo $question[$q_id]['answer'][$val]['count']."<br>";
>
> $question[$q_id]['answer'][$val1]['count']=9999;
>
> echo $question[$q_id]['answer'][$val1]['count']."<br>";
>
>
> will echo correctly the numbers assigned to 'count' of 8888 and 9999
>
> However for this array,
>
> $question['1']=array('type'=>'radio','showresults'=>1,'required'=>0,'questio
> n'=>'Does your community post signs on neglected properties with the
> owner’s name and address clearly visible to all passersby?',
> 'answer'=>array('Yes'=>'Yes','No'=>'No'));
>
> $q_id=1;
> $val='Yes';
> $val1='No';
>
> $question[$q_id]['answer'][$val]['count']=8888;
>
> echo $question[$q_id]['answer'][$val]['count']."<br>";
>
> $question[$q_id]['answer'][$val1]['count']=9999;
>
> echo $question[$q_id]['answer'][$val1]['count']."<br>";
>
> will echo back 'count' as being 8 and 9
>
> I thought in PHP you could extend arrays with new elements dynamically on
> assignment and if you can't, why wouldn't this error out rather than merely
> truncating to one byte?
>
> Or, perhaps, I am thinking about this all wrong???
>
>
> Mike
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
>
More information about the talk
mailing list