More about array indexes CODE
It's possible to define keys only for some of the array items
$array = [1, 2, "name"=>"Three", 4];
print_r($array);
Result
Array
(
    [0] => 1
    [1] => 2
    [name] => Three
    [2] => 4
)
If we set higher index manually, it will continue adding 1 to the max index
$array = [1, 2, 7=>"Three", 4];
print_r($array);
Result
Array
(
    [0] => 1
    [1] => 2
    [7] => Three
    [8] => 4
)
If we set higher index manually, it will continue adding 1 to the index
$array = [1, 2, 7=>"Three", 4=>"forth", 25];
print_r($array);
Result
Array
(
    [0] => 1
    [1] => 2
    [7] => Three
    [4] => forth
    [8] => 25
)
Only integer and string values are allowed as array keys
$array = [
	1    => "a",
    "1"  => "b",
    "-1"  => "-b",
    '1.5'  => "c",
    true => "d",
];
print_r($array);
Result
Array
(
    [1] => d
    [-1] => -b
    [1.5] => c
)
by Valeri Tandilashvili
2 years ago
PHP
Arrays
0
Pro tip: use ```triple backticks around text``` to write in code fences
0
Luka Khitaridze 2 years ago
as i guess, not only integer and string values are allowed as array keys, there is also FLOAT in the last example. (only Numeric and string values are allowed as array keys)
REPLY
Show 1 reply