Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Array Functions

PHP array keys and key casting

How PHP normalizes array keys: numeric strings become integers, floats are truncated, booleans and null are converted, and arrays or objects are illegal keys.

An array key in PHP is always either an integer or a string. Whatever you write between the brackets is converted to one of those two types first, and two keys that convert to the same value are the same key. This is why $a["1"] and $a[1] are the same element, and why $a[true] quietly lands in slot 1.

Knowing the rules explains many surprises: lost entries after merging data, keys that change type when you read them back, or Illegal offset type errors.

PHP
<?php
$a = [];
$a["1"] = "string key";
$a[1]   = "int key";     // overwrites the first
var_dump($a);            // one element, key int(1)

Try it

PHP Keys are normalized

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Next index and illegal keys

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The casting rules

Key you write Becomes
A string that is a canonical decimal integer ("8", "-5", but not "08", "+8", "8 " or "1.5") The integer
Any other string Stays a string
A float Truncated toward zero to an integer; a deprecation notice appears when it has a fractional part (PHP 8.1+)
true / false 1 / 0
null The empty string ""; using null as a key is deprecated since PHP 8.5, write "" instead
An array or an object Not allowed: a TypeError such as "Cannot access offset of type array on array" (older versions say "Illegal offset type"). Use spl_object_id() or serialize() to build a key from an object
A resource The resource's id as an integer, with a warning

Usage notes

  • The next free index. $a[] = $v uses the largest integer key so far plus one. An empty array starts at 0. Starting with PHP 8.3 a first negative key -n is followed by -n + 1; earlier versions jumped to 0.
  • Keys keep their order, not their sorted position. Sort with ksort() when order matters.
  • Integer-like strings change type. After $a["10"] = 1, array_keys($a) returns the integer 10. Compare with == or cast, and avoid strict comparison against string ids.
  • Big integers: strings too large for an integer stay strings.
  • Duplicate keys in a literal: the last one wins, ["a" => 1, "a" => 2] has one element.
  • array_key_exists() and isset() use the same casting. isset() is false for a key whose value is null; array_key_exists() is true.
  • Merging: + keeps the first value for a key, array_merge() renumbers integer keys but keeps string keys.