Вопрос

I want to use strings that come from parsing an external XML document as indices in an array. Since I do not know what kind of strings are held in that document - are there any checks or cleansing processes I should use to make sure there is never a string that php cannot use as a key in the array?

The following answers my question about the size of strings used (limited only by memory available) but not whether there are any special kinds of characters that wouldn't work as keys: Are there size limits to PHP array keys? What are the limits in general for PHP arrays?

Это было полезно?

Решение

Any string is suitable as an array index. There are no special characters you need to care about, since PHP is not trying to parse these strings or otherwise get any "meaning" from them. Strings are binary blobs to PHP, any binary string blob will do.

$array[mcrypt_create_iv(1024, MCRYPT_DEV_RANDOM)] = 'foo';
$array[file_get_contents('bar.jpg')]              = 'bar';
$array["\x00\x01\x02\x03"]                        = 'baz';
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top