Array Functions Flashcards

1
Q

Adding arrays (+)

A

Arrays will be added together and values with the same keys will be overwritten by subsequently added arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

array_change_key_case( array $array [, $case = CASE_LOWER] )

A

Returns an array with all keys from $array lowercased or uppercased. Numbered indices are left as is.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

array_chunk( array $array, int $size [, bool $preserve_keys = false] )

A

Chunks an array into arrays with $size elements. That last chunk may contain less than $size elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

array_column( array $input, mixed $column_key [, mixed $index_key = null ] )

A

$input should be a multi-dimensional array.

Returns the values from a single column of the $input, identified by the $column_key.

Optionally, an $index_key may be provided to index the values in the returned array by the values from the $index_key column of the input array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

array_combine( array $keys, array $values )

A

Creates an array by using the values from the $keys array as keys and the values from the $values array as the corresponding values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

array_count_values( array $array )

A

Returns the number of times each value in $array appears by returning an array in the form “$arrayValue” => “count”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

array_diff( array $array1, array $array2 [, array $… ] )

A

Compares $array1 against one or more other arrays and returns the values in $array1 that are not present in any of the other arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

array_diff_assoc( array $array1, array $array2 [, array $…] )

A

Compares $array1 against $array2 and returns the difference.

Unlike array_diff() the array keys are also used in the comparison.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

array_diff_key( array $array1, array $array2 [, array $…] )

A

Compares the keys from $array1 against the keys from $array2 and returns the difference.

This function is like array_diff() except the comparison is done on the keys instead of the values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

array_diff_uassoc( array $array2, array $array2 [, array $…], callable $key_compare_func )

A

Compares $array1 against $array2 and returns the difference. Unlike array_diff() the array keys are used in the comparison.

Unlike array_diff_assoc() a user supplied callback is used for the indices comparison, not internal function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

array_diff_ukey( array $array1, array $array2 [, array $…], callable $key_compare_func )

A

Compares the keys from $array1 against the keys from $array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

Unlike array_diff_key() a user supplied callback function is used for the indices comparison, not internal function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

array_fill( int $start_index, int $num, mixed $value )

A

Fills as array with $num entries of the value of the $value parameter, keys starting at the $start_index parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

array_fill_keys( array $keys, mixed $value )

A

Fills an array with the values of the $value parameter, using the values of the $keys array as keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

array_filter( array $array [, callable $callback [, int flag = 0] ] )

A

Iterates over each value in the $array passing them to the $callback function.

If the $callback function returns true, the current value from $array is returned into the result array. Array keys are preserved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

array_flip( array $array )

A

Returns an array in flip order, i.e. keys from $array become values and values from $array become keys.

Note that the values of $array need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result.

If a value has several occurrences, the latest key will be used as its value, and all others will be lost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

array_intersect( array $array1, array $array2 [, array $…] )

A

Returns an array containing all the values of $array1 that are present in all the arguments.

Keys are preserved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

array_intersect_assoc( array $array1, array $array2 [, array $…] )

A

Returns an array containing all the values of $array1 that are present in all the arguments.

The keys are also used in the comparison unlike in array_intersect()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

array_intersect_key( array $array1, array $array2 [, array $…] )

A

Returns as array containing all the entries of $array1 which have keys that are present in all the arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

array_intersect_uassoc( array $array1, array $array2, [, array $…], callable $key_compare_func )

A

Returns an array containing all the values of $array1 that are present in all the arguments.

The keys are used in the comparison unlike in array_intersect()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

array_intersect_ukey( array $array1, array $array2 [, array $…], callable $key_compare_func )

A

Returns an array containing all the values of $array1 which have matching keys that are present in all the arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

array_keys( array $input [, mixed $search_value] )

A

Returns an array of values containing the keys from $input.

If $search_value is specified, only keys corresponding to $input values of $search_value will be returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

array_key_exists( mixed $key, array $source )

A

Returns a boolean based on the results of searching $source for a key of $key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

array_map( callable $callback, array $array1 [, array $…] )

A

Returns an array containing all the elements of $array1 after applying the $callback function to each one.

The number of parameters that the $callback function accepts should match the number of arrays passed to array_map()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

array_merge( array $array1, array $array2 [, array…] )

A

Returns an array with all of the array arguments merged, or appended, onto $array1.

25
Q

array_merge_recursive( array $array1 [, array $…] )

A

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

26
Q

array_multisort( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $…] ] ] )

A

Can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.

Associative (string) keys will be maintained, but numeric keys will be re-indexed.

27
Q

array_pad( array $array, int $size, mixed $value )

A

Returns a copy of the $array padded to size specified by $size with value $value. If $size is positive then the array is padded on the right, if it’s negative then on the left. If the absolute value of $size is less than or equal to the length of the $array then no padding takes place.

It is possible to add at most 1048576 elements at a time.

28
Q

array_pop( array &$array )

A

Pops and returns the last value of the $array, shortening the $array by one element.

This function will reset() the array pointer of the input array after use.

29
Q

array_product( array $array )

A

Returns the product of values in an array.

30
Q

array_push( array &$array, mixed $value1 [, mixed $…] )

A

Treats $array as a stack and pushes the passed variables onto the end of $array. The length of $array increases by the number of variables pushed.

Sames as $array[] = $var;

31
Q

array_rand( array $array [, int num = 1] )

A

Picks one or more random entries out of an array and returns the key (or keys) of the random entries.

It uses a pseudo random number generator that is not suitable for cryptographic purposes.

32
Q

array_reduce( array $array, callable $callback [, mixed $initial = NULL ] )

A

Applies iteratively the callback function to the elements of the $array so as to reduce the array to a single value.

$callback looks like this:
callback(mixed $carry, mixed $item)

$carry holds the value of the previous iteration whereas $item holds the value of the current iteration.

33
Q

array_replace( array $array1, array $array2 [, array $…] )

A

Replaces the values of $array1 with values having the same keys in each of the following arrays.

If a key from the first array exists in the second array, its value will be replaced by the value from the second array.

If the key exists in the second array, and not in the first, it will be created in the first array.

If a key only exists in the first array, it will be left as is.

If several arrays are passed for replacement, they will be processed in order, the later arrays overwriting the previous values.

34
Q

array_replace_recursive( array $array1, array $array2 [, array $… ] )

A

Replaces the values of $array1 with the same values from all of the following arrays.

If a key from the first array exists in the second array, its value will be replaced by the value from the second array.

If the key exists in the second array, and not the first, it will be created in the first array.

If a key only exists in the first array, it will be left as is.

If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.

35
Q

array_reverse( array $source [, boolean $preserve_keys] )

A

Returns an array with the order of $source values reversed, including the keys.

Passing TRUE for $preserve_keys will cause the keys to not be reversed.

36
Q

array_search( mixed $needle, array $haystack [, boolean $strict] )

A

Returns the key of the element that contains $needle if it is found in $haystack. Returns FALSE if not found.

If $strict is set to TRUE, type matching occurs.

Be sure to use the strict === operator because if the index of the found value is 0, it may be incorrectly coerced to mean FALSE.

37
Q

array_shift( array &$array )

A

Shift the first value of the $array off and returns it, shortening the $array by one element and moving everything down.

All numerical array keys will be modified to start counting from zero while literal keys won’t be touched.

This function will reset() the array pointer of the input array after use.

38
Q

array_slice( array $array, int $offset [, int $length = NULL [, bool $preserve_keys = false] ] )

A

Returns the sequence of elements from the array $array as specified by the $offset and $length parameters.

39
Q

array_splice( array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ] ] )

A

Removes the elements designated by $offset and $length from the $input array, and replaces them with the elements of the $replacement array, if supplied.

Numeric keys in $input are not preserved.

If $replacement is not an array, it will be typecast to one.

40
Q

array_sum( array $array )

A

Returns the sum of values in an array.

41
Q

array_udiff_assoc( array $array1, array $array2 [, $…], callable $value_compare_func )

A

Computes the difference of arrays with additional index check, compares data by a callback function.

42
Q

array_values( array $input )

A

Returns a numerically-indexed array containing all of the values from $input.

43
Q

arsort( array $subject [, int $sort_flag] )

A

Same as asort(), but in DESC order.

44
Q

asort( array $subject [, int $sort_flag] )

A

Works on associative arrays too - keeps indices matched up to sorted values. Can be handy for numerically-indexed arrays as well.

45
Q

count( array $array )

A

Returns the number of values in the array passed in

46
Q

explode( string $separator, string $subject [, int $limit])

A

Returns an array of pieces of $subject that were split on $separator.

$limit indicates how many splits to perform before just returning the remain portion of $subject.

47
Q

implode( string $glue, array $pieces )

A

Returns a string that is the values of the $pieces array glued together by $glue

48
Q

in_array( mixed $needle, array $haystack [, boolean $strict] )

A

Returns a boolean if the value $needle is found in $haystack.

If optional $strict is passed as TRUE, type matching also occurs.

49
Q

is_array( array $array )

A

Returns a Boolean as to whether the value passed in is an array or not

50
Q

join( string $glue, array $pieces )

A

Returns a string that is the the values of the $pieces array glued together by $glue

51
Q

krsort( array $subject [, int $sort_flag] )

A

Reverse of ksort().

52
Q

ksort( array $subject [, int $sort_flag] )

A

Sorts array keys in ASC order.

53
Q

max( array $numbers )

A

Returns the largest number from $numbers

54
Q

min( array $numbers )

A

Returns the smallest number from $numbers

55
Q

range( mixed $low, mixed $high [, int $step])

A

Returns an array with values starting at $low and ending at $high, with increments between $low and $high being $step apart

56
Q

rsort( array $subject [, int $sort_flag] )

A

Same as sort(), but in DESC order.

57
Q

sort( array $subject [, int $sort_flag] )

A

Returns an array containing ASC sorted values from $subject.

Refer to php.net for $sort_flag values.

58
Q

usort( array $subject [, string $compare_function] )

A

Returns a custom-sorted array in ASC order. $subject is sorted by the user-defined $compare_function, which should accepts two arguments, mixed $a and mixed $b.

$compare_function must return 1 if $a > $b, a -1 if $a < $b, and 0 if $a == $b.

$compare_function is referred to as a “callback function”.