Searching...
Wednesday 17 July 2013

Splicing and Dissecting Arrays

Splicing an Array

The array_splice() function removes all elements of an array (what is an array) found within a specified range, returning those removed elements in the form of an array. Its prototype follows:

array array_splice(array array, int offset [, int length [, array replacement]])

A positive offset value will cause the splice to begin that many positions from the beginning of the array, while a negative offset will start the splice that many positions from the end of the array. If the optional length parameter is omitted, all elements from the offset position to the conclusion of the array will be removed. If length is provided and is positive, the splice will end at offset + length position from the beginning of the array. Conversely, if length is provided and is negative, the splice will end at count(input_array) – length position from the end of the array. An example follows:
$states = array("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Connecticut");
$subset = array_splice($states, 4);
print_r($states);
print_r($subset);

This produces the following (formatted for readability):
Array ( [0] => Alabama [1] => Alaska [2] => Arizona [3] => Arkansas )
Array ( [0] => California [1] => Connecticut )

You can use the optional parameter replacement to specify an array that will replace the target segment. An example follows:
$states = array("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Connecticut");
$subset = array_splice($states, 2, -1, array("New York", "Florida"));
print_r($states);

This returns the following:
Array ( [0] => Alabama [1] => Alaska [2] => New York
[3] => Florida [4] => Connecticut )

Calculating an Array Intersection

The array_intersect() function returns a key-preserved array consisting only of those values present in the first array that are also present in each of the other input arrays. Its prototype follows:

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

The following example will return all states found in the $array1 that also appear in $array2 and $array3:
$array1 = array("OH", "CA", "NY", "HI", "CT");
$array2 = array("OH", "CA", "HI", "NY", "IA");
$array3 = array("TX", "MD", "NE", "OH", "HI");
$intersection = array_intersect($array1, $array2, $array3);
print_r($intersection);

This returns the following:
Array ( [0] => OH [3] => HI )

Note: array_intersect() considers two items to be equal only if they also share the same data type.
Note: Introduced in PHP 5.1.0, the array_intersect_key() function will return keys located in an array that are located in any of the other provided arrays. The function’s prototype is identical to array_intersect(). Likewise, the array_intersect_ukey() function allows you to compare the keys of multiple arrays with the comparison algorithm determined by a user-defined function. Consult the PHP manual for more information.

Calculating Associative Array Intersections

The function array_intersect_assoc() operates identically to array_intersect(), except that it also considers array keys in the comparison. Therefore, only key/value pairs located in the first array that are also found in all other input arrays will be returned in the resulting array. Its prototype follows:

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

The following example returns an array consisting of all key/value pairs found in $array1 that also appear in $array2 and $array3:
$array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");
$array2 = array("50" => "Hawaii", "CA" => "California", "OH" => "Ohio");
$array3 = array("TX" => "Texas", "MD" => "Maryland", "OH" => "Ohio");
$intersection = array_intersect_assoc($array1, $array2, $array3);
print_r($intersection);

This returns the following:
Array ( [OH] => Ohio )

Note: Hawaii was not returned because the corresponding key in $array2 is 50 rather than HI (as is the case in the other two arrays).

Calculating Array Differences

Essentially the opposite of array_intersect(), the function array_diff() returns those values located in the first array that are not located in any of the subsequent arrays:

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

An example follows:
$array1 = array("OH", "CA", "NY", "HI", "CT");
$array2 = array("OH", "CA", "HI", "NY", "IA");
$array3 = array("TX", "MD", "NE", "OH", "HI");
$diff = array_diff($array1, $array2, $array3);
print_r($intersection);

This returns the following:
Array ( [0] => CT )
If you’d like to compare array values using a user-defined function (more about functions in php) , check out the array_udiff() function, introduced in PHP 5.0.2.

Note: Introduced in PHP 5.1.0, the array_diff_key() function will return keys located in an array that are not located in any of the other provided arrays. The function’s prototype is identical to array_diff(). Likewise, the array_diff_ukey() function allows you to compare the keys of multiple arrays with the comparison algorithm determined by a user-defined function. Consult the PHP manual for more information.

Calculating Associative Array Differences

The function array_diff_assoc() operates identically to array_diff(), except that it also considers array keys in the comparison. Therefore, only key/value pairs located in the first array but not appearing in any of the other input arrays will be returned in the result array. Its prototype follows:

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

The following example only returns "HI" => "Hawaii" because this particular key/value appears in $array1 but doesn’t appear in $array2 or $array3:
$array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");
$array2 = array("50" => "Hawaii", "CA" => "California", "OH" => "Ohio");
$array3 = array("TX" => "Texas", "MD" => "Maryland", "KS" => "Kansas");
$diff = array_diff_assoc($array1, $array2, $array3);
print_r($diff);

This returns the following:
Array ( [HI] => Hawaii )

Note: Introduced in PHP 5.0, the array_udiff_assoc(), array_udiff_uassoc(), and array_diff_uassoc() functions are all capable of comparing the differences of arrays in a variety of manners using user-defined functions. Consult the PHP manual for more information.


0 comments:

Post a Comment

 
Back to top!