Why does Multidimentional Arrays replace the first letter of the first key value?

I have a multidimensional array that behaves unexpectedly, and I would like to know why this is, and if there is work. It seems that if I installed something in the first key from the array, it will replace the first letter when I declare the second key in it. (I'm not sure how to describe the behavior correctly, 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']; ?> 

This code is output 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!

+4
source share
5 answers

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]); 
+4
source

Because it is not an array. This is a string.

 $test['hello'] = array(); $test['hello']['jerk'] = "JERK!" 

You are trying to treat the string stored in $test['hello'] as an array. You cannot make $test['hello'] hold both a string ("Hello There") and another array.

+3
source

From http://ca2.php.net/language.types.string .

Characters inside strings can be accessed and changed by specifying the zero offset of the desired character after the string using square brackets, as in $ str [42] ... Non-integer [indices] converted to integer ... only the first character of the assigned string b.

So, to answer your question. $test['hello'] is a string, so string indexing rules will apply. Therefore, $test['hello']['jerk'] = "JERK!"; equivalent to $test['hello'][0] = "J"; because intval 'jerk' is 0 and only the first character "J" assigned string "JERK!" will be used .

After echoing $test['hello'] , you will refer to the entire string, which now has its first character replaced by J Repeating the echo $test['hello']['jerk'] again is equivalent to the echo of $test['hello'][0] , because int 'jerk' has the value 0 , and by the rules of indexing strings $test['hello'][0] returns the first character of $test['hello'] .

In interpreting what you wanted to do, perhaps you wanted to.

 $test['hello'] = "Hello There"; $test['jerk'] = "JERK!"; print_r($test); // array('hello' => "Hello There", 'jerk' => "JERK!") 

Or, to have something multidimensional ...

 $test['message']['hello'] = "Hello There"; $test['message']['jerk'] = "JERK!"; print_r($test); // array('message' => array('hello' => "Hello There", 'jerk' => "JERK!")) 
+3
source

You are trying to declare $test['hello'] as two things: a string and an array. It can only be one or the other.

+1
source

It is worth noting that what happens in the above code example.

Access to the string can be as an array based on indexes. When you try to set the second level of the "array", what it actually does is (since the first level is a string):

 $array['levelOne'] = 'Hello.'; $array['levelOne'][(int)'jerk']; 

This basically gives (or sets) the first character of the string, since "jerk" cast as an integer is 0. If your string could be cast from another integer, then it returned (or set) another character string.

+1
source

Source: https://habr.com/ru/post/1342338/


All Articles