What Is an Example of an Array?


Quick Answer

An example of an array in PHP is array(1 => "a", 2 => "b", 3 => "c"). Other programming languages, such as C++, have different structures.

Continue Reading

Full Answer

In C++, an array's size is statically defined within the code. Keys are always numeric, and the order of the elements defines what the keys are. An example array in C++ is char x[] = { 'a', 'b', 'c' }. Because arrays begin at 0, x[0] is "a", x[1] is "b" and x[2] is "c". C++ uses arrays to represent strings of characters.

In PHP, however, an array is an expandable set of key-value pairs, and both keys and values are largely arbitrary. Therefore, it is possible to have an array where the element "a" is the key and 1 is the value. The only restriction is that the keys must be unique; an attempt to create a new key-value pair with "a" as the key and 2 as the value overwrites the other element where "a" is the key.

It is possible to create a PHP-like array structure in C++, but it requires a structure or object that dynamically allocates memory. Such a construct is not a true array but, rather, a dynamically linked list.

Learn more about Data Graphs

Related Questions