Php-Associative Arrays

Arrays are another basic structure in programming languages. Arrays provide means for
storing a fixed set (or collection) of the same datatype in a convenient way, making
each element of your set indexable by using a unique key.

In the typical "conventional" programming languages, arrays are handled like this:

int my_int_array[786];          // allocate 786 integers in this array
This C code snippet declares an array called my_integer_array, containing 256 integers.
 You can address each of these integers by indexing the array with an ordinal value, for this array in a range from 0 to 255. (C starts counting from 0; the given number in the array definition specifies the number of integers you want to have available.) Indexing looks like this:

int my_integer = my_integer_array[4];
This retrieves the fifth element remember, C starts counting from 0 from the array
 and stores it in my_integer.

Arrays for coding
 Contents of Arrays can be stretched,
 Arrays easily help group related information such as server login detailstogether
 Arrays help write cleaner code.


 Arrays are special variableswith the capacityto store multi values.
 Arrays are flexibilityand can be easily stretchedto accommodate more values
 Numeric arraysuse numbers for the array keys
 Associative arraysuse descriptive namesfor array keys
 Multidimensionalarrays contain other arrays insidethem.
 The countfunction is used to get the numberof itemsthat have been stored in an array
 The is_arrayfunction is used to determinewhether a variableis a valid array or not.
 Other array functions include sort, ksort, assort etc.

 

Due to the nature of compiled languages, you were always bound to the previous
 definition of your variables. If you suddenly needed more than 256 integers in the array above, this was impossible. Of course, you could have defined this variable as a pointer to an integer array and allocated 257 elements for it—but what if you suddenly needed another element? You'd have to allocate new space, copy the old array contents, and free up the old, unused space.

PHP takes a different approach. Because PHP knows no typical variable declarations only type definitions
), new variables are allocated on the fly. Whenever you create a new variable by introducing its
 name into the namespace, you simply create storage space bound to this name—nothing more. The kind of data residing in this space is not restricted to a certain variable type. It can be reinterpreted on the fly, and of course resized, reallocated, whatever.

Take a look at this:

$my_var = 1;
$my_var = "Used to be an integer";
$my_var = array("Oh well, I like arrays better");


The first line creates a new variable $my_var.




PHP will find that an integer is going to be assigned to it; thus it sets the initial
 type of $my_var to integer. The second line, however, overwrites the contents of $my_var
 with a string. Using one of the conventional programming languages, this would have
resulted in an error at compile time, or at least an exception during runtime.
But PHP dynamically changes the type of $my_var to String and reallocates the variable
 so that enough storage space for the string is available. The third line then changes
the type of $my_var once more by creating an array out of it. PHP handles all cases
 transparently without complaining. (We know that other languages out there exist
without strict variable types.

Note: PHP 3.0 doesn't have proper garbage collection. When reallocating a variable,
 memory that's already allocated is not always being reused. In long-term scripts or
 scripts doing heavy processing.

Because formal variable declarations are not needed in PHP variable usage is completely
 dynamic. A special case in PHP's dynamic variable handling is arrays. You probably
know the common array type, the indexed array. Indexed arrays are arrays that are
indexed by ordinal numbers. These ordinal indices typically range from 0 to n, n
being the highest possible index. Languages such as Pascal allow indexing with
different ranges such as from 3 to 18; however, these ranges are transformed back
to 0-based indexes at runtime. The key feature of these ordinally indexed arrays is
that you can compute another index from any given base index. For example, suppose
you want to read out three consecutive array elements, starting from index 2:

$base_index = 2;

for($i = $base_index; $i < $base_index + 3; $i++)
    print("Element $i is $my_array[$i]<br>");
In every iteration of the for() statement, this little snippet computes the next
 index into the array by incrementing $i.

Associative arrays don't have this feature. The special thing about associative arrays
 is that they can be indexed with non-ordinal keys, such as strings, for example.
Every string used as an index has a value associated to it, thus the name associative arrays.
 As you can imagine, giving a string as base index doesn't allow guessing the next valid index
in the array. Thus, associative arrays can't be used to order data elements in an ordinal way.
 You have to know the array keys to retrieve their associated values.

Apart from that, the functions list() and each(), discussed earlier,
can be used to traverse associative arrays.

Indexed arrays are just a special form of associative arrays in PHP. Doing an unset() on
one of the elements in an indexed array will leave all other elements and their ordering
 intact, but produce a nonconsecutive array. See the earlier descriptions of
list() and each() for details.

Multidimensional Arrays
As the name suggests, multidimensional arrays are arrays with more than just one dimension.
 One-dimensional (or single-dimensional) arrays are the form in which arrays are mostly seen:

$my_array[0] = 1;
$my_array[1] = 777;
$my_array[2] = 45;

To index this type of array, you only need one index, which limits the number of possible
values to the range of this index. But it's often very useful to create multidimensional
arrays when handling complex datasets. Typical examples include bitmaps and screen buffers.
 When you look at your monitor, you see at least these days a two-dimensional projection of
 your desktop. The windows, bitmaps, command lines, cursors, pointers—everything is 2D.
To represent this data in a convenient way, you could of course serialize everything
into arrays with a single dimension—but the more appropriate method is to use arrays
with dimensions equal to those of the input data. For example, in order to store a bitmap
a set of pixels for a mouse pointer, you just add another index to your array:

// clear mouse bitmap
for($x = 0; $x < MOUSE_X_SIZE; $x++)
    for($y = 0; $y < MOUSE_Y_SIZE; $y++)
        $mouse_bitmap[$x][$y] = 0;