Characters allowed in php array keys?

I have several php array keys that are filled with lots of weird characters.

Is this allowed? Are there any restrictions for what I cannot use?

+74
arrays php key
May 22 '12 at 5:17
source share
8 answers

According to manual :

The key can be an integer or a string. The value can be of any type.

In addition, the following keystrokes will be performed:

  • Lines containing real integers will be cast from the integer type. For example. the key β€œ8” will actually be stored under 8. On the other hand, β€œ08” will not be selected, since it is not a valid decimal integer.
  • The floats also cast from integers, which means that the fractional part will be truncated. For example. key 8.7 will actually be stored under 8.
  • Bools are also passed in integers, i.e. the true key will actually be stored under 1 and the false key under 0.
  • Null will be passed to an empty string, i.e. the null key will actually be stored under the "" symbol.
  • Arrays and objects cannot be used as keys. This will result in a warning: type of invalid offset.

Leadership:

A string is a series of characters where the character matches a byte. This means that PHP only supports 256-character typing and, therefore, does not offer native Unicode support. More on string type.

In short, any string can be a key. And the string can contain any binary data (up to 2 GB). Therefore, a key can be any binary data (since a string can be any binary data).

Some random (actual) abuse of array keys:

$w = array(chr(0) => 'null byte?', chr(rand(0, 255)) => 'random byte?'); var_dump($w); 
+79
May 22 '12 at 5:21
source share

The key must be a string or an integer. There are a few castings that take place, but I think the manual really helps explain:

The key can be an integer or a string. The value can be of any type.

In addition, the following keystrokes will be performed:

  • Lines containing real integers will be cast from the integer type. For example. the key "8" will actually be stored under 8. On the other, the manual "08" will not be selected, since it is not a valid decimal integer.
  • The floats also cast from integers, which means that the fractional part will be truncated. For example. key 8.7 will actually be stored at 8.
  • Bools are also passed in integers, i.e. the true key will actually be stored under 1 and the false key under 0.
  • Null will be passed to an empty string, i.e. the null key will actually be stored under the "" symbol.
  • Arrays and objects cannot be used as keys. This will result in a warning: type of invalid offset.
+10
May 22 '12 at 5:20 a.m.
source share

I found this answer to search for more information about the problem I had. I used strings with UTF-8 characters that will not work as keys to the array I had.

Something like

 $str = "R&D - Solution"; $arr = array( "R&D - Solution" => "Research" ); echo $arr[$str]; // did not work 

For me there was a (not big or smart) solution.

 $str = md5("R&D - Solution"); $arr = array( md5("R&D - Solution") => "Research" ); echo $arr[$str]; // works! 
+8
Jul 11 '13 at 10:22
source share

Anything you can insert into a PHP string can be used as an array key. There are no valid restrictions on valid characters.

 $a = array(); $x = 'long string of random garage'; echo $a[$x]; // this is ok $x = array(); echo $a[$x]; // not ok 
+5
May 22 '12 at 5:21
source share

PHP array keys can be integers or strings. PHP strings are byte arrays representing byte sequences. There are no other types of strings, and PHP otherwise does not impose any special restrictions on the key strings of the array. In other words: as long as it is a line, everything goes.

+4
May 22 '12 at 5:22
source share

I personally had no problems with unusual characters in the keys of the array. What is and is not legal is not well documented, except to say that the key must be a scalar. It’s best to just try and see.

0
May 22 '12 at 5:21
source share

In addition to all the answers, since they are true: you can use the PSR , that they are some kind of rules between the best programmers for the fact that they have a good and standard coding style.

0
Apr 19 '17 at 13:03 on
source share

Encoding a php page in ANSI "é" will be able to use (Cinéma will not display as Cinà ma). In Notepad ++ just use the menu Encode => Convert ANSI and save

0
Sep 10 '18 at 12:10
source share



All Articles