Saturday, January 31, 2009

Array Functions:

array
Creates a new array.
Format: $array = array(key=>value, key=>value, key=>value,...);
array_count_values
Creates an array that contains a count of the values in the original array.
Format: $array_out = array_count_values($orig_array);
For example, suppose that $orig_array contained the following:
$orig_array[a] = John
$orig_array[b] = Mary
$orig_array[c] = John
$orig-Array[d] = Jose
Then $array_out would contain the following:
$array[John] = 2
$array[Mary} = 1
$array[Jose] = 1

array_diff
Returns $array_out with elements from $array1 that are not present in any other of the specified arrays ($array2, $array3, and so on).
Format: $array_out = array_diff($array1,$array2,$array3 . . .);
array_intersect
Creates an array that contains the elements that are the same (rather than different) in two or more arrays. Format: $simArray = array_intersect($array1,$array2, . . .);
array_keys
Creates an array containing all the keys in the $orig_array. If search_key is included, only keys that match search_key are in the new array.
Format: $array_out = array_keys($orig_array,”search_key”);
For example, suppose that $orig_array contained the following:
$orig_array[a] = CA
$orig_array[b] = OR
$orig_array[c] = TX
Then $array_out would contain the following:
$array_out[0] = a
$array_out[1] = b
$array_out[2] = c
Suppose that search_key= OR, as in the following:
$array_out = array_keys($orig_array,”OR”);
Then $array_out would contain the following:
$array_out[0] = b
array_merge
Merges two or more arrays together. If more than one element has the same non-numeric key, only the last value for the key is added to the output array.
Format: $bigArray = array_merge($array1,$array2, . . .);
array_merge_recursive
Merges two or more arrays. If more than one element has the same nonnumeric key, an array with all the values for the key is added to the output array.
Format: $bigArray = array_merge($array1,$array2, . . .);
array_pop --> Removes and returns the last element in an array.
Format: $element = array_pop($orig_array);
array_push
Adds the specified element(s) to the end of the array. Returns the new size of the array.
Format: $new_size = array_push($orig_array,”el1”,”el2”,”el3”);
array_reverse --> Reverses the order of the items in $orig_array.
array_search --> Searches an array for a value. If value is found, key is returned.
Format: $key = $array_search(“value”,$orig_array);
array_slice
Creates a new array that contains a subset of an existing array. Puts number of elements, beginning with start, into $subArray.
Format: $subArray = array_slice($orig_array,start,number);
array_sum --> Adds all the values in an array.
Format: $sum = array_sum($orig_array);
array_unique --> Removes duplicate elements from an array.
Format: $array_out = array_unique($orig_array);
arsort --> Sorts an array by value in reverse order.
Format: arsort($orig_array);
assort --> Sorts an array by value, keeping the original keys.
Format: asort($orig_array);
compact
Creates an array from the specified variables ($var1, $var2, and so on). The variables can be strings or arrays.
Format: $array_out = compact($var1, $var2, . . .);
Count --> Returns the number of elements in the array.
Format: $size = count($orig_array);
current --> Returns the value of the array element where the pointer is currently located.
Format: $value = current($array);
end --> Moves the pointer to the last element in an array and returns the value.
Format: $value = end($array);
explode
Creates an array containing substrings of a string. The specified separator, sep, which is generally something like a comma or a tab, divides the string into substrings.
Format: $array_out = explode(“sep”,$string);
extract
Creates a set of variables, one for each element of an array. The key for the element is used as the variable name.
Format: extract($array);
implode
Builds a string containing the values of all the elements in an array, separated by the specified separator.
Format: $string = implode($array,”sep”);
in_array
Searches through the values in an array for a specified value. Returns TRUE or FALSE.
Format: $bool = in_array(“value”,$array);
key --> Returns the key of the array element where the pointer is currently located.
Format: $key = key($array);
key_exists
Checks an array to see whether it contains an element with the specified key. Returns TRUE or FALSE.
Format: $bool = key_exists(“key”,$array);
ksort, krsort
Sorts the array by key. ksort sorts in ascending order, and krsort sorts in reverse (descending) order.
Format: ksort($array); krsort($array);
natsort, natcasesort
Sorts an array by value in natural order. The order of the results is n1, n2, n12, n25, rather than n1, n12, n2, n25 in the usual sort. The function natcasesort works the same way but is case-insensitive.
Format: natsort($array); natcasesort($array);
next --> Moves pointer in array to next element.
Format: next($array);
prev --> Moves pointer in array to previous element.
Format: prev($array);
range
Sets up an array with elements spanning a range of values. Possible ranges can be numerical (such as 1–10 or 10@nd1) or alphabetical (such as a–m or m–a).
Format: $array_out = range(start,end);
reset --> Moves pointer to the first element in an array.
Format: reset($array);
sizeof --> Returns the number of elements in an array.
Format: $size = sizeof($array);
sort, rsort
Sorts array by value. sort sorts in ascending order, and rsort sorts in reverse (descending) order.
Format: sort($array); rsort($array);
Format: $array_out = array_reverse($orig_array

No comments:

Post a Comment