The functions in this section operate on arrays. Some of them sort the arrays; some of them help you find and retrieve values from arrays
The array function takes a list of values separated by commas and returns an array. This is especially useful for creating one-off arrays to be passed to functions. Elements will beadded to the array as if you used empty square brackets, which means they are numbered consecutively starting at zero. You may use the => operator to specify index values.
array array_count_values(array data)
The array_count_values function returns counts for each distinct value in the data argument. The returned array is indexed by the values of the data argument. Although the example below uses an array of numbers, array_count_values will count the appearance of elements that contain any other data type.
array array_keys(array data, string value)
The array_keys function returns an array of the keys used in the data array. If the optional value argument is supplied, only the subset of indices that point the given element value are returned.
array array_merge (array data, array data, ...)
The array_merge function takes two or more arrays and returns a single array containing all elements. Elements indexed by integers are added to the new array one at a time, in most cases renumbering them. Elements indexed by strings retain their index values and are added as they are encountered in the input arrays. They may replace previous values. If you are unsure of the indices used in the merged arrays, you can use array_values to make sure all values are indexed by an integer.
boolean array_multisort(array data, integer direction, ...)
The array_multisort function sorts arrays together, as if array were a column in a table. The data argument is an array and the direction argument is one of twoconstants: SORT_ASC or SORT_DESC. These stand for ascending and descending, respectively. If left out, the direction defaults to ascending order, which is smallest to largest. You may specify any number of arrays, but you must alternate between arrays and sort order constants as you do.
The way array_multisort works is similar to the way a relational database sorts the results of a join. The first element of each array is joined into a virtual row, and all elements in a row move together. The arrays are sorted by the first array. In the case where elements of the first array repeat, rows are sorted on the second row. Sorting continues as necessary.
array array_pad(array data, integer size, value padding)
The array_pad function adds elements to an array until it has the number of elements specified by the size argument. If the array is long enough already, no elements are added. Otherwise, the padding argument is used for the value of the new elements. If the size argument is positive, padding is added to the end of the array. If the size argument is negative, padding is added to the beginning.
value array_pop(array stack)
The array_pop function returns the last element of an array, removing it from the array as well. The array_push function compliments it, and array_shift and array_unshift add and remove elements from the beginning of an array.
boolean array_push(array stack, expression entry, ...)
The array_push function adds one or more values to the end of an array. It treats the array as a stack. Use array_pop to remove elements from the stack. The array_shift and array_unshift functions add and remove elements to the beginning of an array.
array array_reverse(array data)
The array_reverse function returns the data argument with the elements in reverse order. The elements are not sorted in any way. They are simply in the opposite order.
value array_shift(array stack)
The array_shift function returns the first element of an array, removing it as well. This allows you to treat the array like a stack. The array_unshift function adds an element to the beginning of an array. Use array_pop and array_push to perform the same actions with the end of the array.
array array_slice(array data, integer start, integer stop)
The array_slice function returns part of an array, starting with the element specified by the start argument. If you specify a negative value for start, the starting position will be that many elements before the last element. The optional stop argument allows you to specify how many elements to return or where to stop returning values. A positive value is treated as a maximum number of elements to return. A negative stop is used to count backward from the last element to specify the element at which to stop.Compare this function to array_merge and array_splice.
array_splice(array data, integer start, integer stop, array insert_data)
The array_splice function removes part of an array and inserts another in its place. The array passed is altered in place, not returned. Starting with the element specified by the start argument, elements are removed until the element specified by the stop argument is reached. If stop is left out, then removal continues until the end of the array. If stop is negative, it references from the end of the array backward. It is possible to specify start and stop values that do not actually remove any values. For instance, the stop value may be positive and less than start. This is a valid way to use array_splice to insert an array without removing any elements.
In place of any removed elements, the array passed as the insert_data argument is inserted if it is supplied. Declaring it is optional, as you may wish simply to remove some elements. If you wish to insert a single element into the array, you do not need to supply an array for insert_data. Compare this function to array_merge and array_slice.<?
boolean array_unshift(array stack, expression entry, ...)
The array_unshift function adds one or more values to the beginning of an array, as if the array were a stack. Use array_shift to remove an element from the beginning of an array. Compare this function to array_pop and array_push, which operate on the end of the array.
array array_values(array data)
The array_values function returns just the array elements, re-indexed with integers.
boolean array_walk(array data, string function)
The array_walk function executes the specified function on each element of the given array. The function must take exactly one element; otherwise an error message is generated. The array elements will be passed by reference, so any change made to them by the specified function will be permanent in the array. The function specified must be one you create, not a built-in PHP function.
The arsort function sorts an array in reverse order by its values. The indices are moved along with the values. This sort is intended for associative arrays.
The asort function sorts an array by its values. The indices are moved along with the values. This sort is intended for associative arrays.
The compact function returns an array containing the names and values of variables named by the arguments. Any number of arguments may be passed, and they may be single string values or arrays of string values. Arrays containing other arrays will be recursively explored. The variables must be in the current scope. This function complements extract, which creates variables from an array.
The count function returns the number of elements in an array. If the variable has never been set, count returns zero. If the variable is not an array, count returns 1. Despite this added functionality, you should use the isset and is_array functions to determine the nature of a variable.<?
integer count(variable array)
The count function returns the number of elements in an array. If the variable has neverbeen set, count returns zero. If the variable is not an array, count returns 1. Despite thisadded functionality, you should use the isset and is_array functions to determine the
nature of a variable.
value current(array arrayname)
The current function returns the value of the current element pointed to by PHP's internal pointer. Each array maintains a pointer to one of the elements of an array. Bydefault it points to the first element added to the array until it is moved by a function such as next or reset.
array each(array arrayname)
The each function returns a four-element array that represents the next value from an array. The four elements of the returned array (0, 1, key, and value) refer to the key and value of the current element. You may refer to the key with 0 or key, and to get the value use 1 or value. You may traverse an entire array by repeatedly using list and each, as in the example below.
The end function moves PHP's internal array pointer to the array's last element. The reset function moves the internal pointer to the first element.
array explode(string delimiter, string data)
The explode function creates an array from a string. The delimiter argument divides the data argument into elements. This function is safe for use with binary strings. The implode function will convert an array into a string.
extract(array variables, integer mode, string prefix)
The extract function creates variables in the local scope based on elements in the variables argument. Elements not indexed by strings are ignored. The optional mode argument controls whether variables overwrite existing variables or are renamed to avoid a collision. The valid modes are listed. If left out, EXTR_OVERWRITE mode is assumed. The prefix argument is required only if EXTR_PREFIX_SAME or EXTR_PREFIX_ALL modes are chosen. If used, the prefix argument and an underscore are added to the name of the extracted variable. Compare this function to compact, which creates an array based on variables in the local scope.
boolean in_array(value query, array data)
The in_array function returns TRUE if the query argument is an element of the dataargument.
//create test data
string implode(array data, string delimiter)
The implode function transforms an array into a string .The elements are concatenated with the delimiter string separating them. To perform the reverse functionality, use explode.
You may use join as an alias to the implode function.
value key(array arrayname)
The key function returns the index of the current element. Use current to find the value of the current element.
boolean krsort(array data)
The krsort function sorts an array by its keys in reverse order—that is, largest values first. The element values are moved along with the keys. This is mainly for the benefit of associative arrays, since arrays indexed by integers can easily be traversed in order of their keys.
boolean ksort(array data)
The ksort function sorts an array by its keys, or index values. The element values are moved along with the keys. This is mainly for the benefit of associative arrays, since arrays indexed by integers can easily be traversed in order of their keys.
The list function treats a list of variables as if they were an array. It may only be used on the left side of an assignment operator. It is useful for translating a returned array directly into a set of variables.
value max(array arrayname) value max(...)
The max function returns the largest value from all the array elements. If all values are strings, then the values will be compared as strings. If any of the values is a number, only the integers and doubles will be compared numerically. The alternate version of the max function takes any number of arguments and returns the largest of them. With this use, you must supply at least two values.
To find the minimum value, use min.
value min(array arrayname) value min(...)
The min function returns the smallest value from all the array elements. If all values are strings, then the values will be compared as strings. If any of the values is a number, only the integers and doubles will be compared numerically. The alternate version of the min function takes any number of arguments and returns the smallest of them. You must
supply at least two values.
value next(array arrayname)
The next function moves PHP's array pointer forward one element. It returns the value at the new element. If the pointer is already at the end of the array, FALSE is returned.
You may use pos as an alias to the current function.
value prev(array arrayname)
The prev function operates similarly to the next function with the exception that it moves backward through the array. The internal pointer to the array is moved back one element, and the value at that position is returned. If the pointer is already at the beginning, FALSE is returned.
array range(integer start, integer stop)
Use range to create an array containing every integer between the first argument and the second, inclusive.
value reset(array arrayname)
Use the reset function to move an array's internal pointer to the first element. The element in the first position is returned. Use end to set the pointer to the last element.
The rsort function sorts an array in reverse order. As with other sorting functions, the presence of string values will cause all values to be treated as strings and the elements will be sorted alphabetically. If all the elements are numbers, they will be sorted numerically. The difference between rsort and arsort is that rsort discards any key values and reassigns elements with key values starting at zero.
The shuffle function randomly rearranges the elements in an array. The srand function may be used to seed the random number generator, but as with the rand function, a seed based on the current time will be used if you do not provide a seed.
This is an alias for the count function.
The sort function sorts an array by element values from lowest to highest. If any element is a string, all elements will be converted to strings for the purpose of comparison, which will be made alphabetically. If all elements are numbers, they will be sorted numerically. Like rsort, sort discards key values and reassigns elements with key values starting at zero.
uasort(array unsorted_array, string comparison_function)
The uasort function sorts an array using a custom comparison function. The index values, or keys, move along with the element values, similar to the behavior of the assort function.
The comparison function must return a signed integer. If it returns zero, then two elements are considered equal. If a negative number is returned, the two elements are considered to be in order. If a positive number is returned, the two elements are considered to be out of order.<?
uksort(array unsorted_array, string comparison_function)
The uksort function sorts an array using a custom comparison function. Unlike usort, the array will be sorted by the index values, not the elements. The comparison function must return a signed integer. If it returns zero, then two indices are considered equal. If a negative number is returned, the two indices are considered to be in order. If a positive number is returned, the two indices are considered to be out of order.
usort(array unsorted_array, string compare_function)
The usort function sorts an array by element values using a custom comparison function. The function must return a signed integer. If it returns zero, then two elements are considered equal.If a negative number is returned, the two elements are considered to be in order. If a positive number is returned, the two elements are considered to be out of order.
PHP Related Interview Questions
|PHP Interview Questions||MySQL Interview Questions|
|PHP+MySQL Interview Questions||Drupal Interview Questions|
|MYSQL DBA Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||Joomla Interview Questions|
|CakePHP Interview Questions||CodeIgniter Interview Questions|
|PHP7 Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.