Core PHP

Array Traversal Functions

Array Traversal Functions
array_product()
int|double array_product(array $qaArray)
This function multiplies all of the values of $qaArray together and returns the product. Conversions are performed as required. Note that the boolean values true and false convert to 1 and 0, respectively. So, we can pass in an array of boolean values and use this function check whether they are all true by checking if the returned product is 1. This logic is valid for empty arrays as well, since they will also return 1.
Argument Description
array $qaArray The product of the values in this array are returned from the function.
array_reduce()
various array_reduce(array $qaArray, function $fnAppliedToArray, various $qInitial = null)
This function returns value generated by calling the function $fnAppliedToArray iteratively, on the values of the array $qaArray. The value $qInitial is used for the first iteration, unless it is null. If array $qaArray is empty, the function simply returns the value $qInitial.
Argument Description
array $qaArray The array that is to be used.
function $fnAppliedToArray This is the function that is called iteratively on each element of $qaArray. The funciton has the form various fnAppliedToArray(various $qPrev, various $qCurr), where $qPrev is the value from previous iteration. If no value is given for $qInitial, the iterations begin at the first element.
various $qInitial This is the initial value that is used for the iteration.
array_sum()
int|double array_sum(array $qaArray)
This function returns the sum of the values in the array $qaArray.
Argument Description
array $qaArray The array from which the sum is calculated.
array_walk()
bool array_walk(array $qaArray, function $fnApply, various $qAdditionalData = null)
This function calls the function $fnApply on each element of $qaArray, optionally passing in $qAdditionalData, if specified. The function returns true, is it was successful.
Argument Description
array $qaArray This is the array from which the values are taken to pass into $fnApply.
function $fnApply This is the funciton that is applied to each entry of the array. Its first argument is the value of current entry, the second is the key of the current entry, and the third, if specified, is the value $qAdditionalData.
various $qAdditionalDat This is the optional additional argument that is passed into the function that is applied to each entry.
array_walk_recursive()
bool array_walk_recursive(array $qaArray, function $fnApply, various $qAdditionalData = null)
This function calls the function $fnApply on each element of $qaArray, optionally passing in $qAdditionalData, if specified. If any of the values of $qaArray are arrays, $fnApply is applied to them recursively. The function returns true, is it was successful.
Argument Description
array $qaArray This is the array from which the values are taken to pass into $fnApply.
function $fnApply This is the funciton that is applied to each entry of the array. Its first argument is the value of current entry, the second is the key of the current entry, and the third, if specified, is the value $qAdditionalData.
various $qAdditionalDat This is the optional additional argument that is passed into the function that is applied to each entry.
count()
int count(Countable|array $qCollection, int $iMode = COUNT_NORMAL)
This function returns an integer that indicates the number of entries in $qCollection, according to $iMode. If $iMode is set to COUNT_RECURSIVE, it will count all of the entries in multidimensional array. If $qCollection is not Countable nor an array, the function returns 1. If $qCollection is null, the function returns 0.
Argument Description
Countable|array $qCollection This is the collection that is to be counted.
int $iMode This is the mode that is used for counting the entries: COUNT_NORMAL or COUNT_RECURSIVE.
extract()
int extract(array &$qarArray, int $iCollisions = EXTR_OVERWRITE, string $sPrefix = "")
This function imports variables and values from the array into the symbol table and returns the number of variables imported into the symbol table.
Argument Description
array &$qarArray This array of variable names and values. If the keys are numeric, only EXTR_PREFIX_ALL and EXTR_PREFIX_INVALID will have an effect.
int $iCollisions This value determines how the key colllisions for varaible names are handled:
  • EXTR_OVERWRITE - Overwrite existing variables.
  • EXTR_SKIP - Do not overwrite existing variables.
  • EXTR_PREFIX_SAME - Prefix a variable name with prefix to avoid the collision.
  • EXTR_PREFIX_ALL - Prefix all variable names with prefix.
  • EXTR_PREFIX_INVALID - Prefix only invalid and numeric variable names with prefix.
  • EXTR_IF_EXISTS - Only overwrite existing variables.
  • EXTR_PREFIX_IF_EXISTS - Only create a prefixed varaible name if a non-prefixed version already exists.
  • EXTR_REFS - Extract variables as references. This can be combined with other values via the bitwise or operator.
string $sPrefix This is the specified prefix that is used when $iCollisions is set to EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS. If the prefixed name is not a valid variable name, it is ignored. Prefixes are separated from name with an underscore character.
list()
array list(various $qVarName1, ...)
This function assigns a set of variables values as if it were an array.
Argument Description
various $qVarName1, ... These are the names of the variables to which the values are assigned.
sizeof()
int sizeof(Countable|array $qCollection, int $iMode = COUNT_NORMAL)
This function returns an integer that indicates the number of entries in $qCollection, according to $iMode. This is the same as count(). If $iMode is set to COUNT_RECURSIVE, it will count all of the entries in multidimensional array. If $qCollection is not Countable nor an array, the function returns 1. If $qCollection is null, the function returns 0.
Argument Description
Countable|array $qCollection This is the collection that is to be counted.
int $iMode This is the mode that is used for counting the entries: COUNT_NORMAL or COUNT_RECURSIVE.
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.