Arrays: functions, methods, multidimensionality
“array” is a data type such as integer or string. If variable $array is array, gettype($array) will display “array”, is_array($array) will display true.
Now let’s talk about array indexes. PHP supports scalar (0,1,2...) indexes as well as associative ("a", "b", "c") array indexes. Associative arrays are used in functions in MySQL:
$row = mysql_fetch_array($result);
print ($row["field1"]. $row["field2"]. $row["field3"]);
Now let’s talk about array creating. First way is function array
$array = array($val1, $val2, $val3);
$array2 = array($key1 => $val1, $key2 => $val2, $key3 => $val3);
In the first case there is a scalar array, in the second case – associative.
We can also create an array having set index and value.
$array[$key] = $val;
If variable $array hasn’t existed it’ll become an array; value $val will be situated in the $key cell. The main advantage is that there is no use in showing the cell’s index.
$new_array[] = "0"; // similar to $new_array[0] = "0"
$new_array[] = "1"; // similar to $new_array[1] = "1"
By the way, very often variable is printed the following way:
print ("test $variable text");
Functions that are used for working with arrays are sizeof (array’s size) and list & each (choose from the array element’s name and its value).
If you want to process all elements of the scalar array you have to use sizeof function and cycle:
for ($i=0;$i<sizeof($array);$i++) {
...
};
If elements begin with known $m you can do as follows:
for ($i=$m;$i<sizeof($array)+$m;$i++) {
...
};
If we have a deal with associative array you can use functions list and each:
while (list($key, $val) = each($array)) {
If you have to print the value to the document use:
print ("...$val...");
If you have to process the array, use its elements:
$array[$key] = somefunction($val);
Array pointer. What is it?
As you can see functions list and each work without index indicating. Pointer that shows the “position” of the program is called Array pointer. It doesn’t stored in any variable and can’t be received anywhere. But if it is necessary it can be moved to the beginning or the end of the array. However these functions are used rarely. But just in case after array elements calling or before working with functions run the command reset($array). In that case pointer will be in the beginning of the array.
Array multidimensionality
Here interpretive program is more flexible than compilable one. As we know array is a data type. Array element can have any data type. Thus, array element can have array data type and be array itself (scalar or associative). In that embedded array there can be a lot of elements. Thus multidimensionality in PHP is realized as a tree-type structure. If embedded arrays have the same size it is “classic” two-dimensional array.
It was theory, and now let’s have a little practice.
$array[$key] has array data type. And all described functions can be applied for that variable. If you want to transform that variable to array use function array:
$array[$key] = array($val1, $val2, $val3);
If you want to create “classic” two-dimensional array you have to use embedded functions array
print (sizeof($array[$key]);
It will print the size of array $array[$key] but not the size of the embedded arrays. Just the same is with list & each.
Indexes’ absence is also applicable for new elements creating.
$array[$key][] = $val1;
$array[$key][] = $val2;
$array[$key][] = $val3;



