Changes

PHP Arrays

1,106 bytes added, 18:51, 1 June 2007
Looping through PHP Array Elements
Value = 123456789
</pre>
 
== Replacing Sections of an Array ==
 
Entire blocks of array elemenst can be modified using the ''array_splice()'' function. The ''array_splice()'' function takes two mandatory and two option arguments. The first argument is the name of the array on which the function is to work. The second argument specifies the index into tyeh array where the splice is to take effect. The optional third argument specifies end point of the splice (if the third argument is omitted the end of the array is assumed). The final argumen tis 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:
 
 
<pre>
$myArray = array ('One', 'Two', 'Three', 'Four', 'Five');
 
$myReplacements = array ('Six', 'Seven', 'Eight');
 
$extract = array_splice ($myArray, 2, 4, $myReplacements);
</pre>
 
The above example uses the ''array_splice()'' function to replace the Three, Four and Five with the Six, Seven and Eight from the $myReplacements array.