Array's content, step by step

Now we will study how the integer keys of arrays change, according to all operations we can perform. This is important to understand associatives arrays and to avoid lot of bugs. The rules are those of PHP for compatibility issue.
After each operation, the content is displayed.

Creating an empty array

array a = array()
array
(
)

Adding an element.

a.push("one")
array
(
 [0]=> one
)

Adding a second array.

a + array("two", "three", "four")
array
(
 [0]=> one
 [1]=> two
 [2]=> three
 [3]=> four
)

Removing the first element.
Keys will be renumbered.

a.shift()
array
(
 [0]=> two
 [1]=> three
 [2]=> four
)

Direct assignment of an element.
Keys remain unchanged.

a[1000] = "thousand"
array
(
 [0]=> two
 [1]=> three
 [2]=> four
 [1000]=> thousand
)

Adding an element at beginning.
This forces renumbering.

a.unshift("x")
array
(
 [0]=> x
 [1]=> two
 [2]=> three
 [3]=> four
 [4]=> thousand
)

Creating two new arrays.
This will force the renumbering.

array a = array("one","two")
array b = array()
b[1000] = "thousand"
a + b
array
(
 [0]=> one
 [1]=> two
 [2]=> thousand
)


If we replace a + b by a.push("thousand") the result is the same.

Lesson to retain

Any change in an array results in a renumbering of keys, but the direct assignment in the form: a[key] = value
You must convince filling array as a dictionay is confusing, use the "push" method instead.