Last Updated: September 09, 2019
·
539
· macodev

Force array key start from 1

If you need an array that starts its key from 1 instead of default zero, this tip can be helpful:

$arr = array(1 => 'foo', 'bar', 'baz', 'meh');
var_dump($arr);

/*
output:

array(4) {
    [1]=> string(3) "foo"
    [2]=> string(3) "bar"
    [3]=> string(3) "baz"
    [4]=> string(3) "meh"
}
*/

Keys after the first are numbered automatically.