// Reassemble dates
$a = $ayear . $amonth . $aday;
$b = $byear . $bmonth . $bday;
// Determine whether date $a > $date b return ($a > $b) ? 1 : -1;
}
usort($dates, 'DateSort');
print_r($dates);
?>
This returns the desired result:
Array ( [0] => 8-18-2002 [1] => 10-01-2002 [2] => 2-16-2003 [3] => 10-10-2003 )
The array_merge() function appends 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. If an input array contains 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 )
array_merge_recursive()
array array_merge_recursive(array input_array1, array input_array2 [, array...]) 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. array_merge() will simply overwrite the preexisting key/value pair, replacing it with the one found in the current input array. array_merge_recursive() will instead merge the values together, forming a new array with the preexisting key as its name. 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.
array_slice()
array array_slice(array input_array, int offset [, int length])
The array_slice() function returns the section of input_array, starting at the key offset and ending at position offset + length. A positive offset value will cause the slice to begin that
many positions from the beginning of the array, while a negative offset value will start the slice that many 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 positions from the beginning of the array. Conversely, if length is provided and is negative, it will end at count(input_array) – length positions 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:
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:
Array ( [0] => Arizona [1] => Arkansas [2] => California )
array_splice()
array array_splice(array input, int offset [, int length [, array replacement]]) The array_splice() function removes all elements of an array, starting at offset and ending at position offset + length, and will return those removed elements in the form of an array.
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 positions from the beginning of the array. Conversely, if length is provided and is negative, the splice will end at count(input_array) – length positions 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:
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 )
array_intersect()
array array_intersect(array input_array1, array input_array2 [, array...])
The array_intersect() function returns a key-preserved array consisting only of those values present in input_array1 that are also present in each of the other input arrays. An example follows:
$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:
Array ( [0] => OH [3] => HI )
Note that array_intersect() considers two items to be equal only if they also share the same datatype.
array_intersect_assoc()
array array_intersect(array input_array1, array input_array2 [, array...])
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
input_array1 that are also found in all other input arrays will be returned in the resulting array.
An example follows:
$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:
Array ( [OH] => Ohio )
Note that Hawaii was not returned because the corresponding key in $array2 is “50” rather than “HI” (as is the case in the other two arrays.)
array_diff()
array array_diff(array input_array1, array input_array2 [, array...])
The function array_diff() returns those values located in input_array1 that are not located in any of the other input arrays. This function is essentially the opposite of array_intersect().
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:
Array ( [0] => CT )
array_diff_assoc()
array array_diff_assoc(array input_array1, array input_array2 [, array...]) 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 input_array1, and not appearing in any of the other input arrays, will be returned in the result array. An example follows:
$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:
Array ( [HI] => Hawaii )