PHP

Php Array And Manipulation Functions In Php

array and manipulation functions

Php Arrays:

PHP Array, it is possible to distinguish three types of php arrays.

  • Indexed arrays where each element of the php array has a number (which corresponds to the usual representation of an php array in other programming languages). Through default, the numbering starts with the index 0.
  • Associative php arrays where each element of the php array is associated with a key represented by a string (which corresponds roughly to a hash array in other programming languages).
  • Mixed php arrays where each element of the array is associated with both an index and a key.

This distinction between arrays is above all “conceptual”. Indeed, their mode of operation is completely identical.

Note:

Index and order in the php array

Indexes should be viewed simply as integer type keys. The value of an index does not prejudge the position of an element in the array. An element can have an index equal to 0 and be at the end of the array. An array is just an (ordered) set of pairs (key, value).

The values ​​of Php array:

The values ​​contained in an array can be of any known type. So an array can store integers, reals, strings, arrays, objects, etc. In addition, the types of values ​​contained in an array can vary from an index (or a key) to the other. The second element of an array may contain a character string, even if the first element contains an object.

Initializing Php array:

The basic syntax for initializing an array is as follows:

$myArray = array();

But, apart from forcing the type of a variable to type array, it is very rare that we were using this clean form. The array initialization is usually done at the same time as its filling. Thus, in the “classic” case of an indexed array, we can for example specify the values associated with the first three indexes by:

$myArray = array (23, “PHP”, 12.4);

Which will then give:

In the case of an associative array, the key and the value must be specified according to the following syntax:

$arrayAge = array (“Fawad khan” => 22, “Shaista” => 20, “xyz” => 34);

To get :

Note:

Use case of mixed php array:

Mixed arrays are used in particular for reading recordings of a database. Thus, the user can call on the value of a field either from its index or from the name of the field.



The subtleties of initializing Php array:

Even in the case of an indexed array, it is possible to specify for which index you are in setting the value (like associative arrays).

Again, it is possible, when initializing an array, to mix this notation with that of an associative array, and even with the “traditional” notation of an indexed array. In the latter case, the values ​​for which the indexes are not specified will have necessarily for index the last affected index + 1 (or by default 0).

$myArray = array (“Value0”, 2 => “Value2”, “Value3”);

will give :

In the case of an indexed or associative array, if, in the initialization string, several values are assigned to the same index or to the same key, the value kept will always be the last encountered. So,

$myArray = array (“Value that will be overwritten”,0 => “Oops … I’m crushing”);

will give :

$myArray[0] = “Oops … I’m crushing “;

Filling Php array:

In a completely classic way, the filling (where the assignment of values) of an array is made by a call of the type:

$myArray [0] = “Value”;

or

$myArray [“key”] = “Value”;

But one of the peculiarities of the PHP language is that the size of the arrays is not fixed. She can therefore be increased over time. So, for an indexed array that was initialized with three elements, it is possible to assign a value to the element of index 10, as shown the following example:

Likewise, for an associative array, it is always possible to add an element with a new key. This is particularly useful for composing a list of items. In addition, to simplify the task, the PHP language allows us not to specify an index or key during an assignment, this in order to use the last affected index + 1 as a clue.

PHP Array manipulation functions:

There are many functions related to processing arrays. Some are intended for browsing the array, others by merging arrays, still others by sorting values ​​or keys, etc. You will also discover in the chapter dedicated to object-oriented programming “The classes, “objects” that there are also objects for manipulating arrays.

Basic Functions of Php Array:

is_array():

Indicates whether a variable is of array type.

Syntax of is_array() function in php array:

boolean is_array (mixed $variable)

where:

$variable: Variable to test.

return: TRUE if the variable is of array type, FALSE otherwise.

count():

Returns the number of elements contained in an array.

Syntax of count() function in php array:

int count(array $array)

where

$array: The array whose number of elements we want to count.

Return: The number of elements in the array. However, if the parameter provided does not does not correspond to an array but to an existing variable, then the value returned is 1. If it is a variable that does not exist, the function returns 0.

If it is an indexed array with values ​​for each index starting at 0, then we can display its content using the following code:

sizeof():

Is the equivalent of count() (same syntax, same behavior).


array_values():

Returns an indexed array containing the values ​​of the elements of a given array (which can allow reindexing an array or converting an associative array to an indexed array).

Syntax of array_values() function in php array:

array array_values ​​(array $array)

where

$array: the Reference array.

return: Indexed array of values ​​found in $array.

See also array_keys(), which can be used to search an array.

array_unique():

Returns an array in which no value occurs more than once (associated with different keys).

Syntax of array_unique() function in php array:

array array_unique (array $array)

where

$array: Reference array.

return: Array in which each value appears only once. This is always the last key associated with a given value which is kept.

array_flip():

Swaps the roles of keys (or indexes) and values ​​in an array. This function is applicable only if the values ​​can become keys or, in other words, if the values ​​are strings or integers.

If the array has several identical values, only the last pair (key, value) will be considered.

Syntax of array_flip() fucntion in php array:

array array_flip (array $array)

where

$array: Array for which keys and values ​​must be swapped.

Return: Array for which the keys and values ​​have been swapped, or FALSE in case of failure.

array_rand():

Returns an index (or a key) or an array of distinct indexes (or keys) taken pseudo-randomly in an array.

Syntax of array_rand() function in php array:

mixed array_rand (array $array, [int $i])

$array: Array on which to search.

$i: Number of keys or indexes to return (by default the number of keys is fixed to 1).

Return: An index (or a key) or an array of distinct indexes (or keys) taken pseudo-randomly in the array.

Basic Search functions of Php array:

array_keys():

Returns an indexed array containing the keys and indexes used in a given array or, optionally, the keys and indexes associated with a given value.

Syntax of array_keys() function in php array:

array array_keys (array $array [, mixed $value])

where

$array: Reference array.

$value: Optional argument specifying which value the keys must be associated with and indexes returned. By default, all the indexes and keys of the array are have returned.

Return: Array of keys and indexes found or an empty array if the value is not found.

array_search():

Returns the first key (or index) of the array, associated with a given value.

Syntax of array_search() function in php array:

mixed array_search (mixed $ value, array $ array [, Boolean $strict])

where:

$value: Value sought.

$array: Array on which the search is carried out.

$strict: Optional argument to set to TRUE if we want the comparison also takes into account the type of value sought. FALSE, by default.

Return: Key (or index) found, FALSE otherwise.

array_key_exists():

Indicates whether or not an array contains a given key.

Syntax of array_key_exists() function in php array:

boolean array_key_exists (mixed $key, array $array)

$key: Key sought.

$array: Array on which the search is carried out.

Return: TRUE if the index exists, FALSE otherwise.

in_array():

Indicates whether or not an array contains a given value.

Syntax of in_array() function in php array:

boolean in_array (mixed $value, array $array [, Boolean $strict])

where

$value: Value sought.

$array: Array on which the search is carried out.

$strict: Optional argument to set to TRUE if we want the comparison also takes into account the type of value sought. FALSE, by default.

Return: TRUE if the value was found in $ array. FALSE otherwise.


Functions for manipulating php array portions:

range():

Returns an array consisting of integers between a start value and an end value.

Syntax of range() function in php array:

array range (int $start, int $end)

where

$start: First integer of the array.

$end: Last integer of the array (must necessarily be greater than or equal to $start).

Return: Array composed of integers between the start value (included) and the end value (included) or an empty array in case of failure.

array_fill():

Returns an indexed array constructed from the repetition of a value.

Syntax of array_fill() function in php array:

array array_fill (int $startindex, int $number, mixed $value)

where

$startindex: First index of the array.

$number: Number of elements in the array.

$value: Value to give to each element of the array.

Return: Array indexed from $startIndex to $startIndex + $number – 1  containing that the value $value.

array_pad():

Returns a completed array (right or left) with a given value to reach a total number of items specified.

Syntax of array_pad() function in php array:

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

where

$array: Reference array.

$size: Size (in absolute value) desired for the result array. If you want to complete the array on the left, then indicate the value in negative.

$value: Value to use to complete the array.

Return: Returns the specified array, completed by the value to reach the size requested. If this (in absolute value) is less than the initial size of the array, the latter is simply not completed, but its size It is not, however, reduced.

array_slice():

Returns an array extracted from another array.

Syntax of array_slice() function in php array:

array array_slice (array $array, int $start [, int $length])

where

$array: Reference array.

$start: Order number of the first element of the array to extract. At first element of the array corresponds to number 0. This sequence number is independent of the index (in particular after a call to the function array_reverse() with the second argument TRUE, the element with index 0 can be found at the end of the array). If $ start is negative, then the count is done starting from the end of the array.

$length: Optional argument specifying the number of elements to extract. Through default, all the following elements of the array are extracted. Yes $length is negative, all the elements up to the element are found at abs ($ length) from the end which are extracted.

Return: Array of extracted elements. Keys are kept but indexes are not.

Example of array_array_slice function:

array_splice ():

Removes elements from an array; optionally, insert elements into the array and returns the array of deleted elements.

Syntax of array_splice() function in php array:

array array_splice (array $array, int $start [, int $length[, array $replacement]])

where

$array: Array to modify.

$start: Order number of the first element of the array to delete. At first element of the array corresponds to number 0. This sequence number is independent of the index (in particular after a call to the function array_reverse () with the second argument TRUE, the element with index 0 can be found at the end of the array). If $start is negative, then the count is done starting from the end of the array. $length: Optional argument specifying the number of elements to delete. Through default, all the following elements of the array are deleted. Yes $length is negative, all the elements up to the element are found at abs ($length) from the end which are removed. $replacement Optional array containing the values ​​to insert in the array from of the element pointed to by $ start. If the number of elements inserted is greater than the number of deleted items, then the rest of the array is shifted. In this case, the keys are kept but not the indexes. By default, the items are simply deleted.

Return: array of deleted elements.



array_chunk():

Returns an indexed array whose values ​​are the pieces of an array divided into parts.

Syntax of array_chunk() function in php array:

array array_chunk (array $array, int $size [, Boolean $mode])

where

$array: An array to be cut into pieces.

$size: Size of pieces (number of elements).

$mode: Specifies whether the keys of the array must be kept: TRUE, the keys are kept. FALSE (default value), each piece of array is reindexed in starting with 0.

Return: Indexed array containing arrays resulting from cutting into pieces of the entrance board.

Functions for manipulating the keys of an array:

array_change_key_case():

Returns an array with modified key case.

Syntax of array_change_key_case() function in php array:

array array_change_key_case (array $array [, int $break])

where

$array: Reference array.

$break: Either one of the following two constants: CASE_LOWER (default value), to put all the keys in lowercase. CASE_UPPER, to put all keys in uppercase.

Return: Array with all indexes in upper or lower case.

array <-> variable conversion functions:

list():

Builds a list of variables from data in an array. This is not a real function, since it is used in the following way:

list ($variable1, $variable2, …) = $array

Syntax of list() function in php array:

void list (mixed $variable1 [, mixed $variable2, …])

where

$variable1: Variable to which must be assigned the first value of the array.

$variable2: Variable to which must be assigned the second value of the array.

… : etc.

extract():

Builds a list of variables from the data in an array, the names of the variables being based on the array keys.

Syntax of extract() function in php array:

int extract (array $array [, int $modeExtraction [, string $prefix]])

where

$array: Array (associative) that we want to use to create variables.

$modeExtraction: Optional argument indicating which strategy to adopt, mainly if the variable you want to create already exists. This argument can take one value among: EXTR_OVERWRITE (default value) replaces the value of the variable by the value found in the array if the key corresponds to a variable existing. EXTR_SKIP ignores the element encountered if the key corresponds to a variable existing. EXTR_PREFIX_SAME precedes the name of the variable by $prefix if the key matches an existing variable. EXTR_PREFIX_ALL always precedes the name of the variable by $ prefix. EXTR_PREFIX_INVALID precedes the name of the variable by $prefix if the key does not allow the creation of a valid variable name (for example if the key is actually an index).

$prefix: Optional argument to be specified if $modeExtraction takes one of the

following values: EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID.

Return: Number of variables generated.


compact():

Returns an associative array created from the names of variables (which will be the keys of the array) and their values.

Syntax of compact() function in php array:

array compact (string $variable1 [, string $variable2, …])

where

$variable1, … : Names of variables to add to the array. These arguments may, in fact, also be arrays containing strings specifying variable names (or arrays). Each argument as array is treated recursively.

Return: Associative array containing all pairs (key, value) corresponding to the given variable names.

Array browse functions:

current()

Returns the value currently pointed to by the internal pointer of the array (without advancing the pointer).

Syntax of current() function in php array:

mixed current (array $array)

where

$array: Array to browse.

Return: Value currently pointed to by the internal pointer of $array. FALSE if the pointer is outside the array.

pos():

Equivalent to current().

key():

Returns the key (or index) of the element currently pointed to by the internal pointer of the array (without moving the pointer forward).

Syntax of key() function in php array:

mixed key (array $array)

where

$array: Array to browse.

Return: Key (or index) of the element currently pointed to by the internal pointer of $array. FALSE if the pointer is outside the array.

reset ()

Returns the internal pointer to the start of the array and returns the first element.

Syntax of reset() function in php array:

mixed reset (array $array)

where

$array: Array to browse.

Return: First element of the array. FALSE if the array is empty.

next ():

Advances the internal pointer of the array and returns the new pointed element.

Syntax of next() function in php array:

mixed next (array $array)

where

$array: Array to browse.

Return: Next element of the array. FALSE if past the end of the array.

Example of  array_next:


prev():

Moves the internal pointer back of the array and returns the new pointed element.

Syntax of prev() function in php array:

mixed prev (array $array)

where

$array: Array to browse.

Return: Previous element of the array. FALSE if the start of the array has been moved.

end():

Places the inner pointer at the end of the array and returns the last element.

Syntax of end() function in php array:

mixed end (array $ array)

where

$array: Array to browse.

Return: Last element of the array. FALSE if the array is empty.

each():

Returns an array containing the key and the value of the element currently pointed to by the internal pointer of the array, then advances the pointer.

Syntax of each() function in php array:

array each (array $array)

where

$array: Array to browse.

Return: Array consisting of four elements: index element 0 and element of key “key” is associated with the key (or index) of the pointed element. of index 1 and the key element “value” is associated with the value of the element point. FALSE if the end of the array is exceeded.

Note:

each () and list () a popular duo competing with foreach The array resulting from the call to each() is often associated with list () in order to manipulate simple strings rather than arrays according to the list schema ($key, $value) = each ($ array) ;. Thus, array are often browsed in the way next :

reset ($array);

while (list ($key, $value) = each ($array)) {

echo “The key $key is associated with the value $value <br />”;

}

Another solution is to use foreach. But it is also possible to do the following:

foreach ($array as $ key => $value)

echo “The key $ key is associated with the value $ value <br />”;

stack functions in php array

It is possible to use PHP arrays as stacks, i.e. as lists in which are stacked together and which can be accessed by taking the first or the last item.



array_push()

Adds one or more elements to the end of the array (above the stack).

Syntax of array_push() function in php array:

int array_push (array $array, mixed $value1, [mixed$value2, …])

where

$array: Array to which you want to add elements.

$value1, … : Values ​​(or data) that you want to add to the array.

Return: Returns the number of elements of the array after addition.

array_pop ():

Returns and deletes the last element of the array (the one on top of the stack).

Syntax of array_pop() function in php array:

mixed array_pop (array $array)

where

$array: Array that we want to “pop”.

Return: The last element of the array or NULL if the array is empty (or if argument is not an array).

array_unshift ():

Adds one or more elements to the start of the array (at the bottom of the stack).

Syntax of array_unshift() function in php array:

int array_unshift (array $array, mixed $value1, [mixed $value2, …])

where

$array: Array to which you want to add elements.

$value1, …: Values ​​(or data) that you want to add to the array. The first one given value will correspond to the first value of the result array.

Return: Returns the number of elements of the array after addition.

array_shift ():

Returns and deletes the first element of the array (the one at the bottom of the stack).

Syntax of array_shift() function in php array:

mixed array_shift (array $array)

where

$array: Array that we want to “pop” from the bottom.

Return: The first element of the array or NULL if the array is empty (or if argument is not an array).

Sort functions in php array:

Reverse sort:

array_reverse ():

Returns an array with the values ​​in the reverse order of the one specified.

Syntax of array_reverse() function in php array:

array array_reverse (array $array, [boolean $i])

where

$array: Reference array.

$i: Optional argument to set to TRUE if the key-> value association must be kept (for an indexed array, the values ​​will keep also their index). By default $i = FALSE; array result is then necessarily an array indexed between 0 and the size of the array -1.

Return: Returns the specified array with the values ​​sorted in reverse order.


Sorting in ascending order of values:

sort():

Sorts the elements of an array in ascending order of value. The keys (or index) are not preserved; the array necessarily becomes an array indexed between 0 and the size of the array-1.

Syntax of sort() function in php array:

void sort (array $array)

$array: Array to sort.

asort ():

Sorts the elements of an array in ascending order of values, while keeping the key association => value.

Syntax of asort() function in php array:

void asort (array $array)

where

$array: Array to sort.

natsort ()

Sorts the elements of an array in ascending, called “natural” order of values. This sorting In not content to compare strings, character by character, but distinguish between numeric parts of alphabetic parts. So, in “natural” order, “word2” appears before “word10”. Keys or indexes are not kept; the array must become a array indexed between 0 and the size of array -1.

Syntax of natsort() function in php array:

void natsort (array $array)

where

$array: Array to sort.

natcasesort ():

Sorts the elements of an array in ascending order, known as “natural”, values ​​without taking into account of the breakage. This sorting doesn’t just compare strings, character by character, but distinguishes numeric parts from alphabetic parts. So, in order “natural”, “word2” appears before “word10”. Keys or indexes are not kept; array becomes an array indexed between 0 and the size of array-1.

Syntax of natcasesort() functio in php array:

void natcasesort (array $array)

where

$array: Array to sort.

Sorting in descending order of values:

rsort ():

Sorts the arrays in descending order of value. The keys (or index) are not preserved; the array necessarily becomes an array indexed between 0 and the size of the array-1.

Syntax of rsort() function in php array:

void rsort (array $array)

where

$array: Array to sort.

arsort ():

Sorts the elements of an array in descending order of values, while keeping the key association => value.

Syntax of arsort() function in php array:

void arsort (array $array)

$array: Array to sort.

Custom sorting of values:

usort ():

Sorts the elements of an array in ascending order of values, determining the order of values ​​from a user-defined function. The keys (or index) are not preserved; the array necessarily becomes an array indexed between 0 and the size of the array-1.


Syntax of usort() function in php array:

boolean usort (array $array, function $comparisonFunction)

where

$array: Array to sort.

$comparisonFunction: Name of the function to use to compare the values. This function must accept two arguments and return: 0 in case of equality, 1 in case of superiority of the first argument over the second and −1 otherwise. boolean TRUE.

Note: This type of function is particularly useful in the case of a somewhat complex comparison (as when, in the example, it is a question of calculating similarity between character strings), and especially when the elements of the array are objects and that the comparisons apply to the attributes of the object.

uasort ():

Sorts the elements of an array in ascending order of values, determining the order of values ​​from a user-defined function. The keys (or index) are not preserved; the array necessarily becomes an array indexed between 0 and the size of the array-1.

Syntax of uasort() function in php array:

boolean uasort (array $array, function $comparisonFunction)

where

$array: Array to sort.

$comparisonFunction: Name of the function to use to compare the values. This function must accept two arguments and return: 0 in case of equality, 1 in case of superiority of the first argument over the second and −1 otherwise. return TRUE.

Sorting in ascending order of keys:

ksort ():

Sorts the elements of an array in ascending key order, while keeping the association key => value.

Syntax of ksort() function in php array:

boolean ksort (array $array)

where

$array: Array to sort.

Return: TRUE.



Sorting in descending order of keys:

krsort ():

Sorts the arrays in descending order of the keys, while keeping the key association =>value.

Syntax of krsort() function in php array:

boolean krsort (array $array)

where

$array: Array to sort.

Return: TRUE.

Personalized key sorting:

uksort ():

Sorts the elements of an array in ascending key order, determining the order of the keys to from a user-defined function.

Syntax of uksort() function in php array:

boolean uksort (array $array, function $comparisonFunction)

where

$array: Array to sort.

$compareFunction: Name of the function to use to compare the keys. This function must accept two arguments and return: 0 in case of a tie, 1 in case of superiority of the first argument over the second and −1 otherwise.

Return: TRUE.

Sorting on linked array:

array_multisort ():

Sorts the array elements according to the order of the first array shown.

Syntax of array_multisort() function in php array:

boolean array_multisort (array $array1 [, int $mode1] [, int $mode2], array $array2 [, array $array3])

where

$array1: Array to be sorted to take as reference for sorting.

$mode1: Sort direction.

SORT_ASC for an increasing sort.

SORT_DESC for a descending sort.

$mode2: Sort option:

SORT_REGULAR for a “traditional” comparison of values.

SORT_NUMERIC for a numeric comparison of values.

SORT_STRING for an alphabetical comparison of values.

$array2, …: Arrays on which permutations performed on $ array1 must be passed on.

Example of array_multisort() function:

Random sort:

shuffle():

Sorts the elements of an array in … random order. In other words, mix the elements of a array. Keys (or indexes) are not kept; the array must become a array indexed between 0 and the size of array -1.

Syntax of shuffle() function in php array:

boolean shuffle (array $array)

where

$array: Array to mix.

Return: TRUE.


Statistics functions of php array:

array_sum ():

Returns the sum of the values ​​of the elements of an array (very useful for calculating the medium).

Syntax of array_sum() function in php array:

mixed array_sum (array $array)

where

$array: Array on which the operation must be performed.

Return: The sum of the values ​​of the array elements, of integer or real type, according to the contents of the array.

array_count_values ​​():

Returns an array specifying the number of occurrences of a value in a given array.

Syntax of array_count_values() function in php array:

array array_count_values ​​(array $array)

where

$array: Array on which the operation must be performed.

Return: Associative array having as keys the values ​​of the array on which was performed the operation and as value the number of occurrences of the key.

Example of array_count_values() function:

Functions implementing several arrays:

Comparison of arrays

array_diff ():

Returns an array showing the values ​​contained in one array, but missing others paintings.

Syntax of array_diff() function in php array:

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

where

$array1: Array whose content we want to compare with those of other arrays.

$array2, …: Arrays in which we search for the possible presence of the values ​​of array 1.

Return: Array presenting all the values ​​contained in $array1 and which were not found in any of the other arrays specified. The keys are kept (identical to that of $array1).

array_intersect ():

Returns an array presenting the values ​​contained in an array and in a set other paintings.



Syntax of array_intersect() function in php array:

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

$array1: Array whose content we want to compare with those of other arrays.

$array2, …: Arrays in which we search for the possible presence of the values ​​of array 1.

Return: Array presenting all the values ​​contained in $array1 and which were found in all other arrays specified. The keys are kept (identical to that of $array1). If there are no values common then an empty array is returned.

array_intersect_assoc ():

Returns an array presenting the values ​​and the keys associated with it, contained in a array and in a set of other arrays.

Syntax of array_intersect_assoc() function in php array:

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

where

$array1: Array whose content we want to compare with those of other arrays.

$array2, …: Arrays in which we search for the possible presence of the values ​​of array 1.

Return: Array showing all the values ​​and keys contained in $array1 and which were found in all other arrays specified. The keys are kept (identical to that of $array1).

Merging arrays:

array_merge ():

Returns an array resulting from the merger of a set of arrays.

Syntax of array_merge() function in php array:

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

where

$array1, …: Arrays to merge.

Return: Array presenting all the values ​​contained in the different paintings. As you browse through the arrays to be merged, the values ​​have been added at the end of the result array. Values ​​linked to keys can be overwritten if the key is encountered more than once.

array_merge_recursive ():

Returns an array resulting from the merger of a set of arrays. In this case, if a given key is encountered several times, the value in the result array will not be the last value encountered, but the fusion (in an array) of the values ​​encountered.

Syntax of array_merge_recursive() function in php array:

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

where

$array1, …: The arrays to merge.

Return: array presenting all the values ​​contained in the different paintings. As you browse through the arrays to be merged, the values ​​have been added at the end of the result array. The values ​​linked to the keys encountered several times are grouped together in a array.

Custom array processing functions:

array_filter ():

Returns a filtered array from a user-written function.

Syntax of array_filter() function in php array:

array array_filter (array $array [, function$filterFunction])

where

$array: Array to filter.

$functionFilter: Function to call to determine whether the element should be kept or not in the result array. This function must take into account a argument, and return TRUE if the element is to be kept, FALSE otherwise. This parameter is (oddly) optional. If no filter function is provided, then the array is rendered as is.

Return: Returns the input array without the elements that have been indicated, by the function $functionFilter, as being to be filtered. The keys and index of array are kept.

Example of array_filter() function:


array_map():

Returns an array resulting from the result of applying a function written by the user, and taking into account the data from one or more arrays.

Syntax of array_map() function in php array:

array array_map (function $function, array $array1 [, array$array2, …])

where

$function: Function to call for each element of arrays $array1,

$array2, etc: This function must take into account as much arguments than arrays provided. $array1, … Arrays containing the values ​​to which the function must apply. If some arrays are shorter than the largest of the arrays, null values ​​complete these arrays. These arrays are treated in the order of their elements (not their index).

Return: Indexed array of the size of the largest of the arrays passed in parameter and having for values ​​the result of the function applied to the elements of arrays passed in parameter.

Example of array_map() function:

array_reduce():

Returns the result of a user-defined operation applied iteratively to the set of values ​​in an array.

Syntax of array_reduce() function in php array:

mixed array_reduce (array $array, function $function [, int $initialValue])

where

$array: Array containing the values ​​to be performed the operation.

$function: Function to call iteratively on each element of the array. This function must take into account two arguments, the first being the result of the calculation found so far, the second being the value to be taken into account for further calculation. The function should return the new obtained result.

$initialValue: Optional argument, specifying the initial value used for the calculation. Through default $initialValue = 0.

Return: The result of the calculation or even the initial value if the array is empty.

Example of array_reduce() function:

array_walk ():

Calls a user function on each element of an array.

Syntax of array_walk() function in php array:

boolean array_walk (array $array, function $function [, mixed$argument])

where

$array: array containing the values ​​on which the operations.

$function: Function to call for each element of the array. This function must take into account two arguments (or three if $ argument is specified), the first argument being the value of the currently considered element, the second being the key (or index) of the currently considered element. The third argument will take the value of $ argument). Warning ! it is not possible in this case to call a PHP function, you must necessarily create your own function.

$argument: Optional argument, passed as third parameter of the function user.

Return: TRUE if the operation went well, FALSE otherwise.

Example of array_walk() function:


Related Article:

Php Functions

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button