Characters allowed in php array keys?

PhpArraysKey

Php Problem Overview


I have some php array keys that are populated with a lot of weird characters.

Is this allowed? Are there any constraints to what I cannot use?

Php Solutions


Solution 1 - Php

According to the manual:

> The key can either be an integer or a string. The value can be of any > type. > > Additionally the following key casts will occur: >

  • Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

The manual again:

>A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. See details of the string type.

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

Some random (valid) abuse of array keys:

$w = array(chr(0) => 'null byte?', chr(rand(0, 255)) => 'random byte?');
var_dump($w);

Solution 2 - Php

The key must be a string or an integer. There are some casts that take place, but I think the manual does a good job of explaining:

> The key can either be an integer or a string. The value can be of any > type. > > Additionally the following key casts will occur: > > * Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other > hand "08" will not be cast, as it isn't a valid decimal integer. > * Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under > 8. > * Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0. > * Null will be cast to the empty string, i.e. the key null will actually be stored under "". > * Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

Solution 3 - Php

I found this answer looking for more information on a problem I had. I was using strings with UTF-8 characters in them, which would not work as keys to an array I had.

Something like

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

The (not big or clever) solution for me was to do this..

$str = md5("R&D - Solution");
$arr = array( md5("R&D - Solution") => "Research" );
echo $arr[$str];  // works!

Solution 4 - Php

PHP array keys can be integers or strings. PHP strings are byte arrays, meaning sequences of bytes. There are no other types of strings and PHP doesn't otherwise impose any special restrictions on array key strings. In other words: as long as it's a string, anything goes.

Solution 5 - Php

Anything you can stuff into a PHP string can be used as an array key. There's no limit on the characters allowed.

$a = array();

$x = 'long string of random garage';
echo $a[$x]; // this is ok

$x = array();
echo $a[$x]; // not ok

Solution 6 - Php

If complex keys are causing an "undefined index" error, you may simply have a "trim" problem.

I was going nuts because a complex key was spitting out the "undefined index" error and I thought maybe it was a syntax violation. The array key causing the error was built from a field from a MySQL database query that I was converting into a key and using in a new array. The key looked like this: pl_1DNKoiJKwotCqAycickBVhTy and here's how the code was constructed.

//new array created from database query
$new_array[$dbquery['fieldname']] = {some value};

//key value found in field of second array
$keyval = $array_two['fieldname'];

//this produced the "undefined index" error
echo $new_array[$keyval];

when, in fact, the $keyval and $dbquery['fieldname'] appeared to be a perfect match (visually verified by echoing both to the browser). The mystery was solved by simply using trim in the second statement like this: $keyval = trim($array_two['fieldname']); Once 'trimmed', php no longer complained.

Hoping this saves some others from some frustrating moments...

Solution 7 - Php

I've personally not had any problems with unusual characters in array keys. What is and isn't legal isn't well documented, other than to say that the key must be a scalar. Your best bet is to just try it and see.

Solution 8 - Php

In addition to all the answers as they are true: You can use PSRs that they are some kind of rules between best programmers for having a nice and standard coding style.

Solution 9 - Php

In php array you cannot use key : 2.3 or decimal numbers

Solution 10 - Php

For this peace of code :

$a = (object) ['@km³' => 123];

This :

error_log($a->@km³);

Produce this error :

PHP Parse error: Syntax error, unexpected '@', expecting T_STRING or T_VARIABLE or '{' or '$' on line 1

But this is working :

error_log($a->{"@km³"});

(with {})

Solution 11 - Php

Encode the php page in ANSI "é" will be able for use (Cinéma wont show up as Cinéma). In Notepad++ just use the menu Encode=>Convert ANSI and save

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncgwebprojectsView Question on Stackoverflow
Solution 1 - PhpCorbinView Answer on Stackoverflow
Solution 2 - PhpMatthewView Answer on Stackoverflow
Solution 3 - PhpRobView Answer on Stackoverflow
Solution 4 - PhpdecezeView Answer on Stackoverflow
Solution 5 - PhpMarc BView Answer on Stackoverflow
Solution 6 - PhpglobalSchmidtView Answer on Stackoverflow
Solution 7 - PhpGordonMView Answer on Stackoverflow
Solution 8 - PhpVeRJiLView Answer on Stackoverflow
Solution 9 - Phpabhishek92View Answer on Stackoverflow
Solution 10 - PhpVictor Daniel Maduro MiquilenaView Answer on Stackoverflow
Solution 11 - PhpFabView Answer on Stackoverflow