Searching...
Friday 12 July 2013

Merging And Slicing Arrays


This section introduces a number of functions (more about functions in PHP) that are capable of performing somewhat more complex array-manipulation (what is an array) tasks, such as combining and merging multiple arrays, extracting a cross-section of array elements, and comparing arrays.

Merging Arrays

The array_merge() function merges arrays together, returning a single, unified array. The resulting array will begin with the first input array parameter, appending each subsequent array parameter in the order of appearance. Its prototype follows:

array array_merge(array array1, array array2 [, array arrayN])

If an input array contains a string (what is a string) key that already exists in the resulting array, that key/value pair will overwrite the previously existing entry. This behavior does not hold true for numerical keys, in which case the key/value pair will be appended to the array. An example follows:

$face = array("J", "Q", "K", "A");
$numbered = array("2", "3", "4", "5", "6", "7", "8", "9");
$cards = array_merge($face, $numbered);
shuffle($cards);
print_r($cards);

This returns something along the lines of the following (your results will vary because of the shuffle):

Array ( [0] => 8 [1] => 6 [2] => K [3] => Q [4] => 9 [5] => 5
[6] => 3 [7] => 2 [8] => 7 [9] => 4 [10] => A [11] => J )

Recursively Appending Arrays

The array_merge_recursive() function operates identically to array_merge(), joining two or more arrays together to form a single, unified array. The difference between the two functions lies in the way that this function behaves when a string key located in one of the input arrays already exists within the resulting array.

Note: array_merge() will simply overwrite the preexisting key/value pair, replacing it with the one found in the current input array, while array_merge_recursive() will instead merge the values together, forming a new array with the preexisting key as its name.

Its prototype follows:

array array_merge_recursive(array array1, array array2 [, array arrayN])

An example follows:

$class1 = array("John" => 100, "James" => 85);
$class2 = array("Micky" => 78, "John" => 45);
$classScores = array_merge_recursive($class1, $class2);
print_r($classScores);

This returns the following:

Array ( [John] => Array ( [0] => 100 [1] => 45 ) [James] => 85 [Micky] => 78 )

Note that the key John now points to a numerically indexed array consisting of two scores.

Combining Two Arrays

The array_combine() function produces a new array consisting of a submitted set of keys and corresponding values. Its prototype follows:

array array_combine(array keys, array values)

Both input arrays must be of equal size, and neither can be empty. An example follows:

$abbreviations = array("AL", "AK", "AZ", "AR");
$states = array("Alabama", "Alaska", "Arizona", "Arkansas");
$stateMap = array_combine($abbreviations,$states);
print_r($stateMap);

This returns the following:

Array ( [AL] => Alabama [AK] => Alaska [AZ] => Arizona [AR] => Arkansas )

Slicing an Array

The array_slice() function returns a section of an array based on a starting and ending offset value. Its prototype follows:

array array_slice(array array, int offset [, int length [, boolean preserve_keys]])

A positive offset value will cause the slice to begin offset positions from the beginning of the array, while a negative offset value will start the slice offset positions from the end of the array. If the optional length parameter is omitted, the slice will start at offset and end at the last element of the array. If length is provided and is positive, it will end at offset + length position from the beginning of the array. Conversely, if length is provided and is negative, it will end at count(input_array) – length position from the end of the array.

Consider an example:

$states = array("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut");
$subset = array_slice($states, 4);
print_r($subset);

This returns the following:

Array ( [0] => California [1] => Colorado [2] => Connecticut )

Consider a second example, this one involving a negative length:

$states = array("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut");
$subset = array_slice($states, 2, -2);
print_r($subset);

This returns the following:

Array ( [0] => Arizona [1] => Arkansas [2] => California )

Setting the optional preserve_keys parameter (introduced in PHP 5.0.2) to true will cause the array values’ keys to be preserved in the returned array.

0 comments:

Post a Comment

 
Back to top!