Changes

PHP Arrays

45 bytes added, 15:18, 13 May 2009
no edit summary
== Create a PHP Array ==
Arrays are created using the ''array()'' function. The ''array()'' function takes zero or more arguments and returns the new array which is assigned to a variable using the assigment assignment operator (=). If arguments are provided they are used to initialize the array with data.
PHP arrays grow and shrink dynamically as items are added and removed so it is not necessary to specify the array size at creation time as it is with some other programming languages.
</pre>
The above ''echo'' command will output the value in index postion position 1 of the array, in this case the string "Yellow".
== Creating an Associative Array ==
A multidimensional PHP array is nothing more than an array in which each array element is itself an array. A multidimensional array can, therefore, be thought of as a table, where each element in the parent array represents a row of the table and the elements of each child array represent the columns of the row.
The following PHP script provides an example of a mutlidimensional multidimensional array where the books array is assigned an array for each element containing title and author information for each book:
<pre>
== Using PHP Array Pointers ==
PHP arrays maintain an internal pointer which that records the current element. This pointer can be changed using the ''next'', ''previous'', ''reset'' and ''end'' functions. The ''reset'' and ''end'' functions move the pointer to the first and last elements of the array respectively. The ''next'' function moves the pointer on to the next array element. The ''prev'' moves the pointer to the previous array element. The ''next'' and ''prev'' functions return false when it is not possible to move any further in the corresponding direction.
Each function takes the name of the array in which the pointer adjustment is to take place as an argument:
array_push($colorArray, "White"); // Add White to end of array
array_pop($colorArray); // Remove White from the end of teh the array
</pre>
</pre>
Will This will result in the following output:
<pre>
</pre>
This will result in the following output:
<pre>
== Replacing Sections of an Array ==
Entire blocks of array elementd element can be modified using the ''array_splice()'' function. The ''array_splice()'' function takes two mandatory and two optional arguments. The first argument is the name of the array on which the function is to work. The second argument specifies the index into the array where the splice is to take effect. The optional third argument specifies the end point of the splice (if the third argument is omitted the end of the array is assumed). The final argument is an optional array containing elements to be used to replace the removed items.
The following example creates and initializes an array, then creates an array of replacement elements before using the splice function:
== Sorting a PHP Array ==
An array can be sorted using the ''sort'' function. A number of different sorts are possible using the ''sort'' function. The function takes two arguments. The first argument is the name of the array. The second indicates the sort algorithm to use. The avialable available algorithms are SORT_NUMERIC, SORT_STRING and SORT_REGULAR. If no sort type is specified, SORT_REGULAR is used.
Similarly array items can can be sorted in descending order using the ''rsort'' function.
For example we can sort our array of color names:
== Getting Information About PHP Arrays & other Array Functions ==
<google>ADSDAQBOX_FLOW</google>
There are number of useful functions which can be used to obtain information about PHP arrays and also some function functions that have not been covered in detail in this chapter. The following table lists these functions and provides descriptions:
<table border="1" cellspacing="0">
<td>print_r<td>Prints the elements of an array to the output stream</td>
<tr>
<td>array_keys<td>Returns an array contaning containing all the keys in an associative array</td>
<tr>
<td>array_search<td>Returns the key for given value in the array if value exists</td>
<tr>
<td>array_values<td>Returns an array contaning containing all the values in an array</td>
<tr>
<td>in_array<td>Returns true or false depending on whether specified value is in array</td>
Arrays will be used often in the course of developing web applications using PHP. Arrays are also often returned from many of the built-in PHP functions so taking the time to learn about them in this chapter will pay off many times over in the future.
 
<google>BUY_PHP_BOTTOM</google>