I have a multidimensional array that is behaving unexpectedly and I would like to know why this is, and if there is a work around. It seems that if I set something in first key of the array, it will replace the first letter of it when I declare a value in it's second key. (I'm not sure how to properly describe the behavior but this code should help somewhat:
<?php
//Declare Array with first key and value
$test['hello'] = "Hello There";
//Echo Value
echo "HELLO TEST: ". $test['hello'] ;
//Declare Multidimensional Array using the first array key and a new key
$test['hello']['jerk'] = "JERK!";
//Echo Values
echo "<br/>HELLO TEST: ". $test['hello'] ;
echo "<br/>JERK TEST : ". $test['hello']['jerk'];
?>
That code outputs as follows:
HELLO TEST: Hello There
HELLO TEST: Jello There
JERK TEST : J
I expect to see
HELLO TEST: Hello There
HELLO TEST: Hello There
JERK TEST : JERK!
Doing this :
$test['hello'] = "Hello There";
You declare that $test['hello']
contains a string.
Then, doing this :
$test['hello']['jerk'] = "JERK!";
You declare that $test['hello']
contains an array ; and no longer a string.
Your $test['hello']
can only contain one thing.
Actually, when you are doing this :
$test['hello']['jerk'] = "JERK!";
As $test['hello']
contains a string (and not an array), I think PHP will try to access this entry : $test['hello'][0]
With 0
being the jerk
string converted to an integer.
And $test['hello'][0]
means the first character of the string that's in $test['hello']
See String access and modification by character in the manual, about that.
There, now, you're trying to put a whole string ("JERK!"
) where there can be only one character -- the first one of the existing string. And that one get overriden by the first character of the string "JERK!"
.
EDIT a while after : and here are the full explanations, with commented code :
// 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's actually done is :
$test['hello'][0] = "J";
// Still echo the whole string, with the first character that's 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's been overriden)
var_dump($test['hello']['jerk']);
// Same as this :
var_dump($test['hello'][0]);
Because it isn't an array. It's a string.
$test['hello'] = array();
$test['hello']['jerk'] = "JERK!"
You're trying to treat the string stored in $test['hello']
as an array. You're not going to be able to make $test['hello']
hold both a string ("Hello There") and another array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With