Performing this action:
$test['hello'] = "Hello There";
You declare that $test['hello'] contains a string.
Then do the following:
$test['hello']['jerk'] = "JERK!";
You declare that $test['hello'] contains an array; and no longer a string.
Your $test['hello'] can contain only one thing.
Actually, when you do this:
$test['hello']['jerk'] = "JERK!";
Since $test['hello'] contains a string (not an array), I think PHP will try to access this entry: $test['hello'][0]
C 0 is a jerk string converted to an integer.
And $test['hello'][0] means the first character of the string, which is in $test['hello']
See String Access and Character Modification in the manual for more on this.
Now you are trying to put the whole line ( "JERK!" ), Where there can be only one character - the first of the existing line. And the one who gets the top character of the first line of the string "JERK!" .
EDIT some time after: and here are the full explanations with comments:
// Assign a string to $test['hello'] $test['hello'] = "Hello There"; //Echo Value var_dump($test['hello']); // Try to assign a string to $test['hello']['jerk'] $test['hello']['jerk'] = "JERK!"; // But $test['hello'] is a string, so PHP tries to make a string-access to one character // see http://fr.php.net/manual/en/language.types.string.php#language.types.string.substr // As 'jerk' is a string, it gets converted to an integer ; which is 0 // So, you're really trying to do this, here : $test['hello'][0] = "JERK!"; // And, as you can only put ONE character where ($test['hello'][0]) there is space for only one , // only the first character of "JERK!" is kept. // Which means that what actually done is : $test['hello'][0] = "J"; // Still echo the whole string, with the first character that been overriden var_dump($test['hello']); // Same as before : here, you're only accessing $test['hello'][0] // (which is the first character of the string -- the one that been overriden) var_dump($test['hello']['jerk']); // Same as this : var_dump($test['hello'][0]);
source share